0

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
midoriha_senpai
  • 177
  • 1
  • 16
  • 1
    Please show us the relevant portion of your code - how you are setting your `month` array and how you are displaying `monthname` in particular. One codeline in isolation becomes a guessing game. – Magoo Jan 19 '18 at 01:18
  • Okay, I added the rest of the code. – midoriha_senpai Jan 19 '18 at 01:21
  • Comment or del (debug only) your first line, maybe this put some light on something. – statosdotcom Jan 19 '18 at 01:33
  • Okay, added the results to the second edit above... – midoriha_senpai Jan 19 '18 at 01:47
  • If you add a line `set mon` just *before* the `set monthname=...` line, batch will display the contents of each variable that starts `mon`. Since we're not aware of you date format, or why you are selecting `3` rather than `2` date digits, we're speculating again. It's a mystery why the `set monthnow=` and further is not being displayed, but it's quite obvious from your published trace that your code is not setting `month[01]` etc, but is setting `month[!m:~-2!]` - the `setlocal enabledelayedexpansion` appears to be being skipped (and is not reported in the trace). On my system, `monthNow=01/` – Magoo Jan 19 '18 at 02:11
  • Try it this way: https://www.dostips.com/DtTipsStringManipulation.php#Snippets.MapLookup – Squashman Jan 19 '18 at 04:42

1 Answers1

0

I found a solution, now I just need to learn why it's a solution, lol. Here is my final, working code [ellipses mine]:

@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
)

The critical part for me was syntax needed on this line: call set monthName=%%month[!!monthNow!!]%%. Everything worked after that. Thanks, everyone for helping to wake up my brain.

midoriha_senpai
  • 177
  • 1
  • 16
  • 1
    I think it should read `Call Set "monthName=%%month[!monthNow!]%%"`, _(single bangs, **`!`**)_. – Compo Jan 19 '18 at 18:31
  • @compo Thanks for your comment! I've tried it with single and double bangs, and I get the correct output both times. Is it going to cause problems down the road or is it only an aesthetics issue? – midoriha_senpai Jan 19 '18 at 20:02
  • Single works and that's the only place you decided that double were needed, you choose! You may also note that I used doublequotes, although it appears to work without, you should learn to follow that example with all of your `Set` statements otherwise that could cause you problems down the road! – Compo Jan 19 '18 at 21:49