errorlevel in a for

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

errorlevel in a for

#1 Post by colargol » 07 Oct 2011 13:43

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: errorlevel in a for

#2 Post by aGerman » 08 Oct 2011 08:36

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

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: errorlevel in a for

#3 Post by colargol » 08 Oct 2011 14:10

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:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: errorlevel in a for

#4 Post by aGerman » 08 Oct 2011 14:54

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

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: errorlevel in a for

#5 Post by colargol » 08 Oct 2011 15:33

Interesting!
Thanks for explanations :wink:

Post Reply