根据星期几工作的批处理脚本停止工作

时间:2017-08-27 14:02:08

标签: windows batch-file

所以昨天我复制/粘贴了一半写了这个批处理文件,根据一周中的某一天自动执行某些任务。它运作得很好,但今天它停了下来。它只是说" Echo是ON"而不是在控制台上写一天中的那一周[1,7]整数,昨天它做得很好......

@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=%%#)
:: Line below is supposed to write day of the week as [1,7] integer at the console. It did that and the rest worked fine yesterday, but not today.
echo %DOW%
pause
@echo off
IF %DOW%==1 (goto monday)
IF %DOW%==2 (goto tuesday)
IF %DOW%==3 (goto wednesday)
IF %DOW%==4 (goto thursday)
IF %DOW%==5 (goto friday)
IF %DOW%==6 (goto saturday)
IF %DOW%==7 (goto sunday)
goto finish
:monday
    :: do something
    goto finish
:tuesday
    :: do something
    goto finish
:wednesday
    :: do something
    goto finish
:thursday
    :: do something
    goto finish
:saturday
    :: do something
    goto finish
:sunday
    :: do something
    goto finish
:finish
    echo finished running

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

wmic输出并不依赖于区域设置。这样可以轻松编写独立于区域设置的代码。 DayOfWeek使用[0 ... 6](其中0 =星期日)。

Findstr [1-7]更改为Findstr [0-6]
IF %DOW%==7 (goto sunday)IF %DOW%==0 (goto sunday)

答案 1 :(得分:0)

您会发现星期日是0天,而不是7

答案 2 :(得分:0)

您的代码似乎有效,因为只有星期日偏离美国/欧盟编号:

Weekday Sun Mon Tue Wed Thu Fri Sat Sun
USdow    0   1   2   3   4   5   6  
EUdow        1   2   3   4   5   6   7

Set /A USdow = EUdow %% 7

我建议使用powershell直接获取工作日名称。 您不必将数字转换为具有多个IF的标签,但可以 使用日期名称本身:

:: Q:\Test\2017\08\27\SO_45905800.cmd
@echo off & Setlocal EnableDelayedExpansion

For /f %%# in ('powershell -NoP -c "(get-date).DayOfWeek"') Do Set "DoW=%%#"
echo:Day of week is: %DoW%

Call :%DoW% passed %DoW%

:finish

goto :Eof

:monday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:tuesday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:wednesday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:thursday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:friday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:saturday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof
:sunday
    :: do something
    echo sub %0 Arguments %*
    goto :Eof

今天的示例输出:

> Q:\Test\2017\08\27\SO_45905800.cmd
Day of week is: Sunday
sub :Sunday Arguments pass Sunday