简体   繁体   English

多次将参数传递给Windows批处理文件

[英]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). 我在将参数传递给批处理文件时遇到问题。参数文件将具有n行数,我想执行bacth来读取第一行,将其作为.bat中的参数并执行。读取下一行它再次作为第二个参数执行。同样,如果在文本文件中找到n行,则应执行n次(例如,如果文本文件有100行,.bat中的循环执行应继续执行100次)。 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" “I1”, “A1”, “A11”

parameter file found 找到参数文件

reading parameters to pass through 读取要传递的参数

"I2","A2","A12" “I2”, “A2”, “A12”

im currently getting only last parameter as output.Please help me to correct the script. 我目前仅获得最后一个参数作为输出。请帮助我更正脚本。

Your first FOR loop is crazy - it attempts to create a variable with a name that matches its value. 您的第一个FOR循环很疯狂-它尝试创建一个名称与其值匹配的变量。 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. 您应该在一个循环中读取所有3个参数。

@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
@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM