Batch file Windows命令提示符批处理文件-未设置变量

Batch file Windows命令提示符批处理文件-未设置变量,batch-file,cmd,Batch File,Cmd,我正在尝试编写一个批处理文件,最终将创建一个基于当前月份的文件名。但是,我已经遇到了一些问题。我试图使用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="F

我正在尝试编写一个批处理文件,最终将创建一个基于当前月份的文件名。但是,我已经遇到了一些问题。我试图使用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

我非常感谢任何指导;我不太熟悉这种编程形式。

我建议您使用另一种方法获取月份名称;例如,通过:


如果您能够从Internet向您的计算机添加脚本,我已经成功地使用了getTimestamp.bat

运行它的结果是:

 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

尝试更改变量的名称,使其使用u而不是-不幸的是,这不起作用。不过谢谢。批处理中没有这样的
elseif
命令。如果不幸,如果仍然无法工作,请使用:
else。谢谢你。我将用修改后的代码更新开场白。没有批处理的
then
word。您还必须将构成“then”部分的每个部分用括号括起来;例如:
如果“%month num%”==“01”(设置月份)=“01”(如果“%month num%”如果“%month num%”==“02”(设置月份txt=“二月”),则其他如果…
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