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 09,2022 at 10:38
In Response to: [General] is there a way to append two lines to many text files? (Ryan)

> is there a way to insert or append or add these two lines to about
> 200 batches?
>
> SET K0="..\pathdata.TXT"
> FOR /F "Tokens=*" %%V IN ('TYPE %K0%') DO (SET K1=%%V)
>
> the combination of the string replacer and #zttag has been working so
> well except for the quotation mark "" replacements. but adding lines
> are my limit.
>
>
> or is there a way to replace @echo off with three lines
> @echo off
> SET K0="..\pathdata.TXT"
> FOR /F "Tokens=*" %%V IN ('TYPE %K0%') DO (SET K1=%%V)
>
> any utilities that can do this with #zttag?


As you are doing the same thing in multiple places, it's probably better to call a separate script with the extra code, to save maintaining it in multiple places.

So for the append case do this
for %%I in (*.cmd) do echo @Call DoStuff.cmd >>%%I


To use #zttag to process a list of tagged files, use this form in the F9 application menu
-------------
@echo off
if "%%1"=="SUB1" goto SUB1
rem #ZTtag #ZTTemp\~ZTtag.cmd -fcall #ZTTemp\ZTW_MENU.BAT SUB1 %1
REM #ZTIfTagged Goto Tagged
goto NoTagged
:Tagged
call #ZTTemp\~ZTtag.cmd
del #ZTTemp\~ZTtag.cmd
goto :eof
:SUB1
shift
Echo %%1
REM Do stuff to the file
Goto :eof
:NoTagged
Echo no tagged files
pause
goto :eof
-------------



For the insert case use a search and replace tool, assuming you have a common pattern to use for the search.
Do a web search there are many of them, or write one yourself.



Or more radically as I presume these scripts are doing similar things, recode them to fewer scripts that use command line parameters, working directory or environment variables to change the behaviour for each use case, and in the new script use the call a subroutine syntax like this, to miniumize repeated code

-------------
@echo off
Echo do stuff
Call :subroutine parameters
Echo do more stuff
goto :eof

:subroutine
Echo %1 %2 %3 %4
goto :eof
-------------


Or to have the helper code in an external script, do something like this

Worker script
-------------
@echo off
Echo do stuff
Call library.cmd function parameters
Echo do more stuff
goto :eof
-------------

library.cmd
-------------
@echo off
goto %1

:function
shift /1
Echo %1 %2 %3 %4
goto :eof
-------------



Ben

584 views      
 

Messages in this Thread

 
96,664 Postings in 12,235 Threads, 350 registered users, 167 users online (0 registered, 167 guests)
Index | Admin contact |   Forum Time: Jun 16, 2024 - 4:13 pm UTC  |  Hits:64,855,496  (65,556 Today )
RSS Feed