Why does for /f not ignore blank lines?

Stese

I'm trying to get a simple value from a for /f loop in a batch file.

Using the command wmic path win32_localtime get dayofweek gives me the output:

C:\sendemail>wmic path win32_localtime get dayofweek
DayOfWeek
1


C:\sendemail>

So, using the following batch script, I should be able to return a result of "1":

set cmd=wmic path win32_localtime get dayofweek
for /f "tokens=1 skip=1" %%Z in ('%cmd%') do set _myday=%%Z
echo Var is %_myday%

But I don't, it sets the variable at least twice, as seen here :

C:\sendemail>set cmd=wmic path win32_localtime get dayofweek

C:\sendemail>for /F "tokens=1 skip=1" %Z in ('wmic path win32_localtime get dayofweek') do set _myday=%Z

C:\sendemail>set _myday=1

 :\sendemail>set _myday=

C:\sendemail>echo Var is
Var is

C:\sendemail>

At first, I wondered why, then I realised the loop is processing the two blank lines... which it shouldn't be. according to this: http://ss64.com/nt/for_cmd.html

Skip SKIP will skip processing a number of lines from the beginning of the file. SKIP includes empty lines, but after the SKIP is complete, FOR /F ignores (does not iterate) empty lines.

Either, it's not working normally, or those lines are not blank, and are filled with something...

At any rate, how do I get just the day of the week result out of this, and ignore the rest?

MichaelS

This slight midification will work:

@ECHO OFF
set cmd=wmic path win32_localtime get dayofweek
for /f "tokens=1 skip=1" %%Z in ('%cmd%') do (
    set _myday=%%Z
    GOTO BREAK
)
:BREAK
echo Var is %_myday%
PAUSE

Simply jump out of the loop after reading the desired line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does QGLWidget only render a blank screen?

From Dev

Why does this code ignore the await and proceed anyway?

From Dev

Why does this output print a blank file?

From Dev

Unix Shell Script: Remove duplicates from line ignore blank lines

From Dev

Why does GLSL ignore return?

From Dev

Why does FloatToStr ignore ThousandSeparator?

From Dev

Why does setInterval() ignore errors?

From Dev

HQL does not ignore values with blank string in column

From Dev

Why does make ignore my modifications?

From Dev

Why does Git ignore these folders?

From Dev

Why does AngularJS ignore $scope field change?

From Dev

Why does SQL Anywhere ignore the @ named parameter?

From Dev

Why does Git ignore the .vs directory?

From Dev

Why does Python requests ignore the verify parameter?

From Dev

Why does this blank game have a leak in Swift

From Dev

Using cat and grep to print line and its number but ignore at the same time blank lines

From Dev

Why does the jvectormap example display a blank page?

From Dev

Why does sudo ignore aliases?

From Dev

why does Twig render blank page?

From Dev

Why does if expression ignore condition in F#?

From Dev

Unix Shell Script: Remove duplicates from line ignore blank lines

From Dev

Why does bash ignore SIGTERM?

From Dev

Why is ag printing blank lines from this file?

From Dev

Why Gulp does not ignore the file?

From Dev

Why does echo -e "\n" give me two blank lines instead of one?

From Dev

Auto-insert blank lines in `tail -f`

From Dev

Why are blank lines printing in my perl script print output

From Dev

Why does target="blank" work?

From Dev

Why does this block of code result in a blank space?

Related Related

HotTag

Archive