簡體   English   中英

Windows命令提示符批處理文件-未設置變量

[英]Windows Command Prompt Batch File - Variable Not Getting Set

我試圖寫一個批處理文件,最終將創建一個基於當前月份的名稱的文件。 但是,我已經遇到了一些問題。 我試圖使用if / elseif語句將包含月份名稱的變量設置為字符串,但沒有成功。 它僅在屏幕上回顯“”,而不是月份名稱。

    @echo OFF
set month-num=%date:~4,2%
if "%month-num%" == "01" then set month_txt="January" else if "%month-num%" == "02" then set month_txt="February" else if "%month-num%" == "03" then set month_txt="March" else if "%month-num%" == "04" then set month_txt="April" else if "%month-num%" == "05" then set month_txt="May" else if "%month-num%" == "06" then set month_txt="June" else if "%month-num%" == "07" then set month_txt="July" else if "%month-num%" == "08" then set month_txt="August" else if "%month-num%" == "09" then set month_txt="September" else if "%month-num%" == "10" then set month_txt="October" else if "%month-num%" == "11" then set month_txt="November" else if "%month-num%" == "12" then set month_txt="December"
@echo "%month_txt%"
timeout /t -1

我真的很感謝任何指導; 我對這種編程形式不太熟悉。

我建議您使用另一種方法來獲取月份名稱。 例如,通過數組

@echo OFF
setlocal EnableDelayedExpansion

rem Initialize month names based on two-digits numbers
set i=100
for %%a in (January February March April May June July August September October November December) do (
   set /A i+=1
   set month[!i:~1!]=%%a
)

set month-num=%date:~3,2%

set month-txt=!month[%month-num%]!

echo "%month-txt%"
timeout /t -1

如果您能夠從Internet將腳本添加到計算機,則可以成功使用http://www.dostips.com/forum/viewtopic.php?t=4847上的 getTimestamp.bat。

C:>type t.bat
CALL getTimestamp.bat -F "{MONTH}"

FOR /F "usebackq tokens=*" %%m IN (`getTimestamp.bat -F "{MONTH}"`) DO (SET "MONTH_NAME=%%m")
ECHO MONTH_NAME is set to %MONTH_NAME%

運行它的結果是:

 9:34:08.95  C:\src\bat
C:>call t.bat

 9:34:15.87  C:\src\bat
C:>CALL getTimestamp.bat -F "{MONTH}"
August
MONTH_NAME is set to August

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM