Page 1 of 1

errorlevel in a for

Posted: 07 Oct 2011 13:43
by colargol
Hi

this problem makes me crazy :oops:

wavi.exe is a command line tool that write to sdterr and return errorlevel 0 in case of success

so this works:

Code: Select all

wavi.exe test.avs 2>&1
echo %errorlevel%


and this:

Code: Select all

for /f "usebackq tokens=4 delims=,: " %%a in (`"wavi.exe test.avs 2>&1"`) do (
   echo channels=%%a
)

echo %errorlevel%
works perfectly but errorlevel keep the value it has before the loop
So I can't guess if my command worked or not.

Is this due to the loop. How should I do?
Thanks for help!
colargol

Re: errorlevel in a for

Posted: 08 Oct 2011 08:36
by aGerman
You could try a small workaround

Code: Select all

for /f "usebackq tokens=2 delims=,:" %%a in (`"wavi.exe test.avs 2>&1"`) do (
  for /f %%b in ("%%a") do set /a n=%%b
)
echo channels=%n%


For my understanding if %n% equals 0 it failed.

Regards
aGerman

Re: errorlevel in a for

Posted: 08 Oct 2011 14:10
by colargol
I had to do something like this.
Thank you aGerman :)

But I still don't understand why errorlevel value never changes if I put the command line into a for :shock:

Re: errorlevel in a for

Posted: 08 Oct 2011 14:54
by aGerman
As far as I figured out the command line inside of a FOR /F loop is either executed in another thread of the same cmd instance (in case of internal commands) or it is executed in another instance of cmd. However it seems there is no chance to pass the errorlevel to the parent process.

Regards
aGerman

Re: errorlevel in a for

Posted: 08 Oct 2011 15:33
by colargol
Interesting!
Thanks for explanations :wink: