嵌套扩展的批处理脚本不起作用

Batch script with nested expansions not working

我正在尝试使用嵌套变量扩展的结果设置一个变量。 month 是一个月份名称数组,从一月 (01) 到十二月 (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 is off." 我发现了一个 S.O。页面上有很好的信息,但建议不起作用。也许我错过了什么?这是该页面:Why is delayed expansion in a batch file not working in this case?

编辑:

添加了其余代码。我最初并没有全部添加,因为我保留了从 set newName... 开始并以 move 命令结束的原始形式(可能不正确)的代码,直到我成功打印了月份名称。我不仅收到一条 "ECHO is off." 消息,而且其他命令中的 none 变量都在工作,我想如果我解决了一个问题,我就可以解决其余的问题。但现在你拥有了一切!

@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 monthName=... 上方添加 set mon 并注释掉 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

我找到了一个解决方案,现在我只需要了解为什么它是一个解决方案,大声笑。这是我最终的工作代码[省略我的]:

@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!!]%%。之后一切正常。谢谢大家帮我脑补一下。