Windows CMD - use numbered variables

123 Views Asked by At

If this has been asked here before, I'm not finding it.

I think I know a way around my actual problem, but it'd get a bit unwieldy; so this is more of a puzzle, I guess.

I want to set 12 variables:

set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December

If I use a CMD file to ask what's contained in %01%, I'm returned the name of the CMD file (which is always %0) plus the number 1. For %10% through %12%, I receive the content of %1, which in my case so far has been empty, plus the second digit of the variable name.

I'm moving files based on date to a storage server. The files are named "2015-01-01 Blah, Blah, Blah.txt", for instance. I can use code obtained here elsewhere to find out the year as token1 and the month as token2.

The storage destination has a folder for 2015, and inside that are subfolders, "01 - January", "02 - February", and so on.

I can always do a thorough "if token2=" each freaking month, for every single file, but I'm wondering whether there's a way to streamline the logic: Move %1 token1\token2, where 01 is expanded to "01 - January", without needing to IF my way through each month.

Code to start from:

@echo off
set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December

:: set /p cr_mo= "Pick a month, any month!  "
:: echo CR_Mo=%CR_Mo%
:: echo Destination folder:  %CR_Mo%

echo 01=%01%
echo 02=%02%
echo 03=%03%
echo 04=%04%
echo 05=%05%
echo 06=%06%
echo 07=%07%
echo 08=%08%
echo 09=%09%
echo 10=%10%
echo 11=%11%
echo 12=%12%
1

There are 1 best solutions below

3
On BEST ANSWER

easiest way is to use delayed expansion:

@echo off
setlocal enabledelayedexpansion
set 01=01 - January
echo 01=!01!