1

I m having issues in passing parameters to a batch file.A parameter file will have n number of lines and i want to execute the bacth to read the first line,take that as a parameter in .bat and execute.Read the next line take it as second parameter execute again.Likewise it should execute n number of times if it finds n number of lines in text file.(For example if the text file have 100 lines the loop execution in .bat should continue for 100 times). I have script like,

@echo off setlocal enabledelayedexpansion set file1=D:\Batch\parm.txt set /a cnt=0 for /f "tokens=*" %%a in (%file1%) do ( set %file1% =%%a echo !%file1%! ) FOR /F "tokens=1 delims=|" %%G IN (%file1%) DO set a1=%%G FOR /F "tokens=2 delims=|" %%K IN (%file1%) DO set a2=%%K FOR /F "tokens=3 delims=|" %%I IN (%file1%) DO set a3=%%I echo parameter file found echo reading parameters to pass through echo (%a1%,%a2%,%a3%)>>D:\Batch\output.txt goto break :break set /a cnt+=1 exit /b 

my parameter file has input as

"India"|"Australia"|"Africa" "I1"|"A1"|"A11" "I2"|"A2"|"A12" 

my output should be:

parameter file found

reading parameters to pass through

"India","Australia","Africa"

parameter file found

reading parameters to pass through

"I1","A1","A11"

parameter file found

reading parameters to pass through

"I2","A2","A12"

i m currently getting only last parameter as output.Please help me to correct the script.

2 Answers 2

1

Your first FOR loop is crazy - it attempts to create a variable with a name that matches its value. I don't see how it serves any purpose.

Your logic is all wrong for each parameter. You read the entire file for the first parameter in a loop. When that loop is finished, you only have one parameter value for the last line found. You then do the same process for the 2nd and 3rd parameters. That can't work.

You should read all 3 parameters in a single loop.

@echo off setlocal set "file1=D:\Batch\parm.txt" if exist "%file1%" ( echo parameter file found echo reading parameters to pass through set /a cnt=0 for /f "usebackq tokens=1-3 delims=|" %%A IN ("%file1%") do ( echo (%%A,%%B,%%C^) set /a cnt+=1 ) )>d:\batch\output.txt echo cnt=%cnt% exit /b 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help.Still i m unable to form loops.Output is something like parameter file found reading parameters to pass through "India","Australia","Africa" "I1","A1","A11" "I2","A2","A12" All parameters at same time.I want each parameter to execute one at a time along with echo statements.Count is showing as one.Would that be a issue for loop not repeating.Thanks once again
@Gpgsam - There was a bug - the ) character had to be escaped in the ECHO statement. All fixed now. Count is now 3 as expected.
0
@echo off setlocal enabledelayedexpansion set file1=D:\Batch\parm.txt set /a cnt=0 for /f "tokens=*" %%a in (%file1%) do ( set %file1%=%%a echo !%file1%! ) FOR /F "tokens=1,2,3 delims=|" %%G IN (%file1%) DO set a1=%%a&set a2=%%b&set a3=%%c echo parameter file found>>D:\Batch\output.txt echo reading parameters to pass through>>D:\Batch\output.txt echo (%a1%,%a2%,%a3%)>>D:\Batch\output.txt goto break :break set /a cnt+=1 exit /b 

1 Comment

Thanks.Unable to form loops still.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.