Domanda

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:

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.

Any ideas on how to make it work? Thanks!

È stato utile?

Soluzione

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top