I'm trying to process the output of the command wmic os get version
in a cmd.exe batch for loop. Seems simple enough:
for /f %%G in ('wmic os get version') do (
echo %%G
)
But the output is:
Version
10.0.19044
ECHO is off.
That last line, ECHO is off.
means that the %%G
was "blank".
Ok so no problem, just test for that:
for /f %%G in ('wmic os get version') do (
if [%%G] neq [] echo %%G
)
Output is the same:
Version
10.0.19044
ECHO is off.
So is it not blank? After much experimentation, it contains a single carriage return character aka <CR>
aka 0x0D aka \r (not to be confused with line feed aka <LF>
aka 0x0A aka \n; also note that <CR><LF>
pair is the standard windows/dos line terminator), as this code demonstrates:
for /f %%G in ('wmic os get version') do (
echo /////////%%G\123\
)
Output:
/////////Version\123\
/////////10.0.19044\123\
\123\////
So the question is, how to detect/process that last line with the carriage return. Can I detect it with an if
comparator? Can I string substitute it? Can I use some for
delimiter? What can I do? My immediate need is to just not print that last line for neatness, but one can imagine any arbitrary processing wanted on the text, including for /f
with tokens and other delims.