2

I would like to use the numerical part of the output of "wmic path win32_localtime get dayofweek" to number backup files. Something along the lines of:

FOR /F "tokens=2 delims=\n" %%DoW IN ('wmic path win32_localtime get dayofweek') DO (echo %%DoW)

Except that the above does not work. Any help would be much appreciated.

aelfinn
  • 21
  • 2
  • 2
  • Seems I have a solution: www.is.gd/gRtm4 -- is there a non-Assembler solution, though? ;> – aelfinn Nov 09 '10 at 15:11
  • Note that that won't work on 64-bit Windows, as you cannot run 16-bit programs there. I'd consider it a poor solution. – Joey Nov 09 '10 at 22:37

3 Answers3

3

In the newer MS Windows OS's, which includes Vista and Windows 7, the "date" command does not return a day of week but you can still put the day into a variable by using the following in a batch script:

@echo off & Setlocal 
Set "_=mon tues wed thurs fri sat sun" 
For /f %%# In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [1-7]') Do ( 
        Set DOW=%%#)
:: now lets display the day of week on the screen 
echo "%DOW%"
pause

For Windows 2K and XP you can parse the day from the "date" command by using the following in a batch script:

@echo off
echo.|date|find "is:" >Get.bat
findstr "is:" get.bat > Str
for /f "tokens=5 delims= " %%d in (str) do set day=%%d
del get.bat
del str
:: echo day of week to the screen
echo Today is %day%
pause
Von Willis
  • 31
  • 1
2

Variables in a FOR loop can only be single character. The delims you have indicates a literal backslash and "n" instead of a newline.

@echo off
SETLOCAL enabledelayedexpansion
SET /a count=0
FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
    if "!count!" GTR "0" GOTO next
    ECHO %%D
    SET /a count+=1
)
:next
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Thanks, I’ll give this a try as soon as I can. In the meantime, can I ask a) where I could have found the information regarding single-character variables in FOR loops, b) what your second line is for, and c) whether there is a term for ‘newline’ to be used in ‘delims’? :) – aelfinn Nov 09 '10 at 23:49
  • `help for` has the information regarding single character variables: "%variable Specifies a single letter replaceable parameter." The second line enables delayed expansion of variables so `!count!` is evaluated when it's executed (while `%count%` would be evaluated when the line containing it is read and not re-evaluated at each step of the loop). See 'help setlocal' and 'help set'. Supposedly `^T` (Ctrl-t) or `¶` (Alt+0182) can be used for newlines, but apparently not in the case of `delims`. If you can, use PowerShell instead of CMD for scripts. – Dennis Williamson Nov 10 '10 at 00:07
1
for /f "skip=1 tokens=2 delims=," %i in ('wmic path win32_localtime get dayofweek /format:csv') do set DOW=%i
echo %DOW%
jscott
  • 24,484
  • 8
  • 79
  • 100