ZTree.com  | ZEN  | About...  

 Index   Back

[General] is there a way to append two lines to many text files?   [General]

By: Ben Kent       
Date: Jan 11,2022 at 09:36
In Response to: [General] is there a way to append two lines to many text files? (Ryan)

You havn’t described what the script is meant to do, so making some guesses.

> trying to apply AA= on the top of BB= was a mistake for my batch
> level. i always leave AA= to #ZTTtag. i dont know much about how BB='s
> operators work but i got a help for it. at least i could modify it to
> what and where to insert
> i dont know why 1. didnt work
> 1. for /L %%A in (1+SKIP 1 end) this one should have worked
> 2. for /L %%A in (1+%SKIP% 1 end) number + string numer dont mix
Neiher of those wikk work, FOR /L does not support maths, only literals

> this kind of way was over my head.

> and this line too. set "line1="& set /p "line1=" it didnt stop me
> for a prompt.
I think the contents of file are being used as the input for set /p "line1=" lines
And the output of the () block is written to !file!.~tmp

If the script is meant to read line 3 and 4 of the file and make that the new content, then what about this solution?

@echo off
Set Line1=
for /F "skip=4 tokens=*" %%I in ( %1 ) do if not defined Line1 set “Line1=%%I”
Set Line2=
for /F "skip=5 tokens=*" %%I in ( %1 ) do if not defined Line2 set “Line2=%%I”
>”%~1.~tmp” echo %Line1%
>>”%~1.~tmp” echo %Line2%
>nul move /y ”%~1.~tmp” "%~1"

Or if the script is meant to delete numbered lines, then try this
@echo off
setlocal EnableDelayedExpansion
If “%~1”==”” (
Echo no file supplied
Goto :eof
If exist "%~1.~tmp" del "%~1.~tmp"
Set DropLine=5
Set Count=0
for /F "tokens=*" %%I in ( %1 ) do (
Set /a Count+=1
if !Count! LSS %DropLine% echo %%I>>"%~1.~tmp"
for /F "skip=%DropLine% tokens=*" %%I in ( %1 ) do >>"%~1.~tmp" echo %%I
>nul move /y ”%~1.~tmp” "%~1"

This has the quotes in the value, which means you don't quote later, but makes it harder to to string operations
Set var="value"

This does not have quotes in the variable, which can make a number of things simpler, but you need to remember to quote if needed on use
Set "var=value"

Some comments below regaring mismatched brackets

> i had fun finishing this batch in the mist and made it work
> thanks for your help as always.
> AA=
> for %%A in ("*.bat") do (
> set file="%%~A"
> )
> BB=
> @echo off &setlocal
> set "file=%~1"
> set /A SKIP=4
> set /A REST=SKIP+1
> setlocal EnableDelayedExpansion
The forum eat some of this line
> "!file!.~tmp" (
Get the number of lines in the file in %%i
> for /f %%i in ('type "!file!"^|find /c /v ""') do (
> for /L %%A in (1 1 %SKIP%) do (
Read the line from STDIN, i.e. from !file!
> set "line1="& set /p "line1="

Better to write this line without the bracket
> echo(!line1!
> )
> REM -------------------------------------------
Because of the (), better to delete the lines in this sectio, instead of echo them
> REM -------------------------------------------
What is this for?
> .
> for /L %%A in (%REST% 1 %%i) do (
> set "line2="& set /p "line2="
Better to write this without the bracket
> echo(!line2!
> )
> )
> )
> >nul move /y "!file!.~tmp" "!file!"
> endlocal


Messages in this Thread

96,664 Postings in 12,235 Threads, 350 registered users, 205 users online (0 registered, 205 guests)
Index | Admin contact |   Forum Time: Jun 16, 2024 - 2:53 pm UTC  |  Hits:64,850,072  (60,132 Today )
RSS Feed