How to? Abort on error DO loop

samintz

Scott Mintz
May 20, 2008
1,574
27
Solon, OH, USA
If I have a command line DO that is operating on a list is it possible to abort the DO if the command errors?
E.g.
Code:
do f in /l a b c (somecommand %f)

If somecommand errors while working on a or b, I'd like the DO to stop and not even try c.
 
Does 'somecommand' return a nonzero exit code when it fails?
That doesn't seem to matter (see below). I thought maybe you could do it manually by looking at _DO_ERRORS, but no. What kind of errors does _DO_ERRORS count?

Code:
v:\> do z in /L xx yy ( dir %z & echo %_? %_do_errors)

 Volume in drive V is DATA         Serial number is 6e8a:6d1f
TCC: (Sys) The system cannot find the file specified.
 "V:\xx"
                   0 bytes in 0 files and 0 dirs
     492,625,555,456 bytes free
2 0

 Volume in drive V is DATA         Serial number is 6e8a:6d1f
TCC: (Sys) The system cannot find the file specified.
 "V:\yy"
                   0 bytes in 0 files and 0 dirs
     492,625,555,456 bytes free
2 0

You can force it:

Code:
v:\> do z in /L xx yy ( dir %z & if %_? != 0 leave )

 Volume in drive V is DATA         Serial number is 6e8a:6d1f
TCC: (Sys) The system cannot find the file specified.
 "V:\xx"
                   0 bytes in 0 files and 0 dirs
     492,625,555,456 bytes free

v:\> do z in /L xx yy ( grep foo %z & if %? != 0 leave )
grep: xx: No such file or directory

v:\>
 

Similar threads