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)

:: Set the month number you want to look up
:: 8 is August

:: 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:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.