I am a Perl programmer, but I need to make a Windows batch file for a specific task.
The script needs to read the input file line by line and copy it to the output file. However, in part, it must switch output files.
For reading the input file and output to the file in the general case, the solution in Jeb's next article works best. (Other solutions do not work well when the input has strange characters or long lines) Batch files: how to read a file?
However, I cannot get it to work when I change the output file in the middle. The problem is probably related to how DelayedExpansion works.
When I run the following script, the outfile at the end is still a.txt, and new_variable is not set.
@echo off del /f/q a.txt del /f/q b.txt del /f/q c.txt del /f/q out.txt set outfile=a.txt SETLOCAL DisableDelayedExpansion FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ in.txt"`) do ( set "var=%%a" SETLOCAL EnableDelayedExpansion set "var=!var:*:=!" IF "!%var!" == "/************************** PLUGINS SECTION *************************/" ( set outfile=b.txt set new_variable=this echo "FOUND! Value of outfile should change to b.txt" ) echo( !outfile! !var! >>!outfile! ENDLOCAL ) echo Outfile: %outfile% echo New Variable: %new_variable%
batch-file
Nicholas Jul 10 '12 at 7:11 2012-07-10 07:11
source share