Yury wrote:"Eol" must be applied to the entire string, regardless of the declared delimiters
Why on earth would you say that, when your own experiment disproves your assumption
CMD.EXE is so poorly documented - usually a lack of documentation, but sometimes the documentation is simply wrong. I've never seen the EOL feature properly documented in any official MS docs.
The proposed FOR /F rules I laid out fully predict the observed behavior.
But... I never realized FOR /F treated the absence of an un-skipped line as an error condition
Thanks for your examples - This could be useful
As long as at least one line is not skipped, then there is no error:
Code: Select all
C:\test>(for /f %a in ('echo hello^&echo( ') do @echo %a) && echo PASS || echo FAIL
hello
PASS
But if all lines are skipped, FOR /F raises an error:
Code: Select all
C:\test>(for /f %a in ("") do @echo %a) && echo PASS || echo FAIL
FAIL
C:\test>(for /f %a in (" ") do @echo %a) && echo PASS || echo FAIL
FAIL
C:\test>(for /f %a in (";hello") do @echo %a) && echo PASS || echo FAIL
FAIL
C:\test>(for /f "tokens=2" %a in ("hello") do @echo %a) && echo PASS || echo FAIL
FAIL
C:\test>(for /f "skip=1" %a in ("hello") do @echo %a) && echo PASS || echo FAIL
FAIL
For these next examples, remember that (CALL) without a space raises an error, and (CALL ) with a space clears any error.
An empty FOR /F loop does not automatically set ERRORLEVEL to non zero, even though an error is raised:
Code: Select all
C:\test>(call )
C:\test>for /f "tokens=2" %a in ("") do @echo %a)
C:\test>echo %errorlevel%
0
But ERRORLEVEL is set if || is activated:
Code: Select all
C:\test>(call )
C:\test>(for /f "tokens=2" %a in ("") do @echo %a) || rem
C:\test>ECHO %errorlevel%
1
You must be careful, because an error condition can exist upon completion of FOR /F if the last executed DO command raised an error:
Code: Select all
C:\test>(for /f %a in ("hello") do @echo %a & (call) ) && echo PASS || echo FAIL
hello
FAIL
So if you want to test if FOR /F had at least one non-skipped line, then your last DO command should clear any error:
Code: Select all
C:\test>(for /f %a in ("hello") do @echo %a & (call) & (call ) ) && echo PASS || echo FAIL
hello
PASS
Dave Benham