We help IT Professionals succeed at work.

For /F - To create dynamically created .bat file

elwayisgod
elwayisgod asked
on
Medium Priority
143 Views
Last Modified: 2019-10-15
Hi Experts,

Not sure For /F is correct solution but I think it is.

Years.csv
Years.jpg
Desired Result for the 'Result.bat' file:
Desired_Result.jpg
My current For /F which is not working.  Never even makes it to the pause for some reason.

@echo off

set AppName=Fin

FOR /f "tokens=1 delims," %%G IN ("D:\Years.csv" DO @echo "call epmautomate exportData %AppName%_DATA_%%G %AppName%_LEV0_DATA_%%G.zip" > "D:\Result.txt"

pause

exit /b

Open in new window

Years.csv
Comment
Watch Question

Author

Commented:
The idea here is my Years.csv file will change as they add years and it's dynamic.  So then I can dynamically create the 'Results.bat' script so there is no manual maintenance in future years when they add a year to the application.  Just trying to make it more hands off is all.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
You didn't close the round brackets in the for /f, you forgot the equal sign after delims, and you need to skip the first line:
@echo off
setlocal

set AppName=Fin

FOR /f "skip=1 tokens=1 delims=," %%G IN ('type "D:\Years.csv"') DO (
	echo call epmautomate exportData %AppName%_DATA_%%G %AppName%_LEV0_DATA_%%G.zip
) > "D:\Result.txt"

pause

exit /b

Open in new window

Author

Commented:
Close as it only gives me the last line FY27 in Results.bat.  I tried changing the > to >> also and same result.

call epmautomate exportData Fin_DATA_FY27 Fin_LEV0_DATA_FY27.zip

Author

Commented:
Wait... the >> worked :)

Author

Commented:
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Sorry, missed the %AppName% part:
@echo off
setlocal

REM set AppName=Fin

FOR /f "skip=1 tokens=1 delims=," %%G IN ('type "D:\Years.csv"') DO (
	echo call epmautomate exportData %%AppName%%_DATA_%%G %%AppName%%_LEV0_DATA_%%G.zip
) >> "D:\Result.txt"

pause

exit /b

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
To save having to clear out the result file before appending to it, I like to roll the whole FOR loop into an outer set of braces, so that the total results of the FOR loop are sent to the file, and can be done with simple redirection rather than concatenation.

Also, I'd also recommend using variables for things like files names and other constants just to make readability and future maintenance better.

@echo off
setlocal

set YearsFile=D:\Years.csv
set ResultFile=D:\Result.csv
set AppName=Fin

(
    for /f "skip=1 tokens=1 delims=," %%G in ('type "%YearsFile%"') do (
        echo call epmautomate exportData %AppName%_DATA_%%G %AppName%_LEV0_DATA_%%G.zip
    )
) > "%ResultFile%"

pause

exit /b

Open in new window


»bp

Author

Commented:
Thanks fellas.  Seem to be working great.  I'll do more testing shortly.