Gpg sam
Gpg sam

Reputation: 11

pass parameters to windows batch file multiple times

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.

Upvotes: 1

Views: 2369

Answers (2)

dbenham
dbenham

Reputation: 130819

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

Upvotes: 1

cure
cure

Reputation: 2688

@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

Upvotes: 0

Related Questions