我正在尝试使用嵌套变量扩展的结果设置变量。month是一组月份名称,从 January (01) 到 December (12)。monthNow保存当前月份的代码(例如,01)。这是我的代码:
@echo off
setlocal EnableDelayedExpansion
set curTimestamp=
for %%a in ("_p"
"%date:~3,3%"
"%date:~7,2%"
"%date:~10,4%"
"%time:~0,2%"
"%time:~3,2%"
) do set curTimestamp=!curTimestamp!%%~a
set curTimestamp=!curTimestamp: =!
rem Get the current month string
set m=100
for %%m in (January
February
March
April
May
June
July
August
September
October
November
December
) do (
set /a m+=1
set month[!m:~-2!]=%%m
)
set monthNow=%date:~3,3%
set monthNow=!monthNow: =!
set monthName=!month[%monthNow%]!
set newName=DimData%curTimestamp%.csv
echo !monthName!
md Z:\...\%monthName% 2> nul
move /y "Z:\...\DimData.csv" ^
"Z:\...\%monthName%\%newName% 2> nul"
这个存储在monthName中的结果应该是一月。但是,它实际上是在返回“ECHO 已关闭”。我找到了一个包含很好信息的 SO 页面,但是这些建议不起作用。也许我错过了什么?这是该页面:为什么在这种情况下批处理文件中的延迟扩展不起作用?
编辑:
添加了其余代码。我最初没有添加它,因为我将代码从set newName...
移动命令开始和结束都保留在其原始形式(可能不正确)中,直到我成功打印了月份名称。我不仅得到“ECHO 已关闭”。消息,但其他命令中的变量都没有工作,我想如果我解决了一个问题,我可以解决剩下的问题。但现在你拥有了一切!
@statosdotcom 的编辑 2:
注释掉第一行后的控制台输出:
set curTimestamp=!curTimestamp!_p
set curTimestamp=!curTimestamp! 01
set curTimestamp=!curTimestamp!18
set curTimestamp=!curTimestamp!2018
set curTimestamp=!curTimestamp!17
set curTimestamp=!curTimestamp!34
(
set /a m+=1
set month[!m:~-2!]=January
)
(
set /a m+=1
set month[!m:~-2!]=February
)
(
set /a m+=1
set month[!m:~-2!]=March
)
(
set /a m+=1
set month[!m:~-2!]=April
)
(
set /a m+=1
set month[!m:~-2!]=May
)
(
set /a m+=1
set month[!m:~-2!]=June
)
(
set /a m+=1
set month[!m:~-2!]=July
)
(
set /a m+=1
set month[!m:~-2!]=August
)
(
set /a m+=1
set month[!m:~-2!]=September
)
(
set /a m+=1
set month[!m:~-2!]=October
)
(
set /a m+=1
set month[!m:~-2!]=November
)
(
set /a m+=1
set month[!m:~-2!]=December
)
ECHO is on.
为@magoo 编辑 3:
set mon
添加上面set monthName=...
并注释掉 ECHO 语句后的控制台输出:
monthNow=01
month[01]=January
month[02]=February
month[03]=March
month[04]=April
month[05]=May
month[06]=June
month[07]=July
month[08]=August
month[09]=September
month[10]=October
month[11]=November
month[12]=December