如何将 DayOfWeek 值放入文件名中?

如何将 DayOfWeek 值放入文件名中?

我想使用“wmic path win32_localtime get dayofweek”输出的数字部分来对备份文件进行编号。类似以下内容:

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

但上述方法无效。任何帮助都将不胜感激。

答案1

在较新的 MS Windows 操作系统(包括 Vista 和 Windows 7)中,“date”命令不会返回星期几,但您仍然可以通过在批处理脚本中使用以下命令将星期几放入变量中:

@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

对于 Windows 2K 和 XP,您可以使用批处理脚本中的以下内容从“date”命令解析日期:

@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

答案2

循环中的变量FOR只能是单个字符。delims您有的表示文字反斜杠和“n”,而不是换行符。

@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

答案3

for /f "skip=1 tokens=2 delims=," %i in ('wmic path win32_localtime get dayofweek /format:csv') do set DOW=%i
echo %DOW%

相关内容