简体   繁体   English

如何在批处理文件脚本中循环打印程序的返回值?

[英]How do I print the return value of a program in a loop in a batch file script?

The task is to run a program(the same program) for ten times and output for each run the exit code(the return value of the main function). 任务是运行一个程序(同一程序)十次,并在每次运行时输出退出代码(主函数的返回值)。 So I want to run a batch file(Windows), like this: 所以我想运行一个批处理文件(Windows),像这样:

FOR /l %%x IN (1,1,10) DO (
    AutomatedTest.exe cip.log
    ECHO %ERRORLEVEL%
)

The code above should do it if you're thinking intuitively, but it doesn't work because the code that it's running is actually: 如果您凭直觉思考,上面的代码应该可以执行此操作,但是它不起作用,因为它正在运行的代码实际上是:

(
    AutomatedTest.exe cip.log
    ECHO 0
)

and this piece is executed 10 times. 并执行10次。

Any ideas on how to make it work? 关于如何使其运作的任何想法? Thanks! 谢谢!

What you need is delayed variable expansion: 您需要的是延迟变量扩展:

FOR /l %%x IN (1,1,10) DO (
    AutomatedTest.exe cip.log
    ECHO !ERRORLEVEL!
)

To enable delayed variable expansion precede your batch with SETLOCAL ENABLEDELAYEDEXPANSION or start command shell with CMD.EXE /V:ON . 要启用延迟的变量扩展,请在批处理之前使用SETLOCAL ENABLEDELAYEDEXPANSION或使用CMD.EXE /V:ON启动命令外壳。

Another approach is using subroutines: 另一种方法是使用子例程:

FOR /l %%x IN (1,1,10) DO CALL :Test
GOTO :EOF

:Test
AutomatedTest.exe cip.log
ECHO %ERRORLEVEL%
GOTO :EOF

Yet another approach is to use IF ERRORLEVEL . 另一种方法是使用IF ERRORLEVEL

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

相关问题 如何从批处理文件中的函数返回值? - How do I return a value from a function in a batch file? 如何在批处理程序中打开文件? - How do i open a file in a program with batch? 如何在批处理脚本中将命令的输出打印到文件? - How can i print output of a command to a file in Batch Script? 如何遍历文件夹中的文件以创建参数列表以传递到Windows批处理文件中的另一个脚本中? - How do I loop through files in folder to create a parameter list to pass into another script in a Windows batch file? 如何成功回复For循环到批处理脚本? - How do I Successfully Echo a For Loop Into a Batch Script? 批处理脚本在 IF DO 循环中检查文件中的字符串 - Batch Script check a file for a string in a IF DO loop 如何将一个变量从被调用的批处理文件返回给调用者? - How do I return a variable from a called batch file to the caller? 在批处理文件中,如何遍历带有空格的字符串? - In a batch file, how do I loop over strings with spaces in them? 使用批处理程序,如何在csv文件中循环和读取行,获取匹配值,然后将其作为密码分配给PDF - Using a batch program, how to loop and read lines in a csv file, get the matching value, then assign it as password to a PDF 我如何制作一个类型的批处理程序 - how do i make a batch program that types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM