I'm trying to set a variable with the result of a nested variable expansion. month is an array of month names, from January (01) to December (12). monthNow holds the current month's code (e.g., 01). Here is my code:
@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"
This result stored in monthName should be January. However, it is actually returning "ECHO is off." I found a S.O. page with good information on it, but the suggestions aren't working. Maybe I'm missing something? Here's that page: Why is delayed expansion in a batch file not working in this case?
EDIT:
Added rest of code. I didn't add it all originally because I left the code starting from set newName...
and ending with the move command in its original, probably incorrect, form until I was successful in getting the month name printed. Not only do I get an "ECHO is off." message, but none of the variables in the other commands are working, I figured that if I solved one problem, I could figure out the rest. But now you have it all!
EDIT 2 for @statosdotcom:
Console output after commenting out the first line:
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.
EDIT 3 for @magoo:
Console output after adding set mon
above set monthName=...
and commenting out the ECHO statement:
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