Batch file look-up table for month names

The look up table goes in months.bat:

:: one record per line
:: = is the delimiter
@echo 1=jan
@echo 2=feb
@echo 3=mar
@echo 4=apr
@echo 5=may
@echo 6=jun
@echo 7=jul
@echo 8=aug
@echo 9=sep
@echo 10=oct
@echo 11=nov
@echo 12=dec

Given a month number, you can then look up the month abbreviation as follows:

:: Enable numeric comparison in if statements (Windows NT or better)
@SETLOCAL ENABLEEXTENSIONS

:: Set the month number you want to look up
:: 8 is August
@SET MN=8

:: Look up the months abbreviation (e.g. 8 is aug, 9 is sep, 10 is oct)
@FOR /F "tokens=1,2 delims==" %%i IN ('months.bat') DO @IF %%i EQU %MN% SET MW=%%j

:: The month abbreviation is now the %MW% variable
@ECHO The month abbreviation corresponding to %MN% is %MW%.
  • ‘months.bat’ executes that file and pipes the output to the FOR /F command
  • delims== sets the delimiter to the equals sign
  • tokens=1,2 means you want the first two tokens (e.g. 1 and aug for 1=aug=August)
  • %%i means the first token will go in %%i, second in %%j, etc

Alternate solutions are welcome. I’m still a bit unclear on the difference between %%i, %i, and %i%.

Useful links:

Published by

Leons Petrazickis

I'm a full-stack developer at IBM Digital Business Group. I do Ruby, Node, Python, Hadoop, Spark, as well as web scale devops with Docker and Terraform. My opinions are my own.