我尝试使用嵌套变量扩展的结果设置变量。 月 是一个月份名称数组,从1月(01)到12月(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 中的结果应为1月。然而,它实际上正在返回" ECHO已关闭。"我发现了S.O.页面上有很好的信息,但建议不起作用。也许我错过了什么?这是该页面的Why is delayed expansion in a batch file not working in this case?
添加了其余代码。我之前没有添加所有内容,因为我将代码从set newName...
开始,并以原始的,可能不正确的形式结束移动命令,直到我成功获取打印的月份名称。我不仅得到了#ECHO关闭。"消息,但其他命令中的变量都没有工作,我想如果我解决了一个问题,我可以弄清楚剩下的。但现在你拥有了一切!
注释掉第一行后的控制台输出:
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.
在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
答案 0 :(得分:0)
我找到了一个解决方案,现在我只需要了解为什么它是一个解决方案,哈哈。这是我最后的工作代码[省略号]:
@echo off
if exist Z:\...\DimData.csv (
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: =!
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: =!
call set monthName=%%month[!!monthNow!!]%%
set newName=DimData!curTimestamp!.csv
md Z:\...\%date:~10,4%\!monthName! 2> nul
move /y "Z:\...\DimData.csv" ^
"Z:\...\%date:~10,4%\!monthName!\!newName!" 2> nul
)
对我而言,关键部分是此行所需的语法:call set monthName=%%month[!!monthNow!!]%%
。之后一切都有效。谢谢,每个人都帮助唤醒我的大脑。