How to get the exit code from the inner command in a for ... in ('command') ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

How to get the exit code from the inner command in a for ... in ('command') ?

#1 Post by jfl » 10 Nov 2016 09:09

Hello,

this is the first in a series of 3 problems that are blocking me right now...

I need to get the exit code from the inner command in a for /f "delims=" %%l in ('command') do (loop).
I can't rely on the number of loops, ie. the presence or absence of output on stdout, because an empty output is a valid output, distinct from when invalid arguments trigger an error message on stderr.

I tried testing the errorlevel at the end of the for loop, or at the beginning the first loop, but this does not seem to work.
I also tried doing things like 'command ^|^| echo something', but did not find any workable solution.

Any idea on how to do that?


jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: How to get the exit code from the inner command in a for ... in ('command') ?

#3 Post by jfl » 10 Nov 2016 10:04

Thanks!

I had tried a very similar solution already, but failed.
The critical factor I had not thought about was the carets added in %%^^errorlevel%%.
I suppose this works by preventing the variable expansion in the for context?

Post Reply