2017-08-27 75 views
0

所以昨天我半複製/粘貼了一半這個批處理文件,根據星期幾來自動執行一些任務。它工作得很好,但今天停止了。它只是說,「回聲接通」,而不是寫在本週日的[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 

我該如何解決這個問題?

+0

它不是[1..7 ],但[0..6](其中0 =星期日)。星期天改變'findstr'和'if' – Stephan

+0

它工作,但是如何?我使用非星期一作爲本週開始的非美國語言環境。這是否因計算機區域設置而改變? –

+0

'wmic'輸出不依賴區域設置(可以很容易地編寫獨立於區域設置的代碼) – Stephan

回答

0

wmic輸出不依賴區域設置。這使得編寫獨立於區域設置的代碼變得容易。 DayOfWeek使用[0 ... 6](其中0 =星期日)。

變化Findstr [1-7]Findstr [0-6]
IF %DOW%==7 (goto sunday)IF %DOW%==0 (goto sunday)

0

你會發現那個星期天是0而不是7

0

您的代碼似乎工作,因爲只有週日從美國偏離/ EU編號:

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來直接將平日名。 您不必到數字轉換爲標籤與幾個國際單項體育聯合會,但可以 使用天名稱本身:今天的

:: 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