views:

59

answers:

2

I would like to create an environment variable to hold a filename something like:

PREFIX-2010-AUG-09.zip

I can get close if I use something like this:

SET filename=PREFIX-%date:~-4,4%-%date:~-7,2%-%date:~0,2%.zip

Result:

PREFIX-2010-08-09.zip

but in this case, I get the month as two digits (08).

Is there any easy trick in Windows batch files to get the three-letter month abbreviation from the numeric month (e.g. 08 for "AUG" = August) ??

Update: this needs to be run on a Windows 2008 R2 Server, and yes, if someone can show me a PowerShell solution, that would work, too :-) Thanks!

+1  A: 

You could always do the number-to-text translation by hand, like:

if %MM%==01 set MM=Jan
if %MM%==02 set MM=Feb
if %MM%==03 set MM=Mar
if %MM%==04 set MM=Apr
etc.
Dev F
+1  A: 

This is something like a look up table:

set month_01=JAN
set month_02=FEB
set month_03=MAR
@rem ...

set number=02

for %%a in (month_%number%) do call set month_as_text=%%%%a%%

echo %month_as_text%

The value in %number% in the for loop is used to dereference the matching variable name.

Or even shorter:

set number=02

for /f "tokens=%number%" %%m in ("JAN FEB MAR APR ...") do set month_as_text=%m

echo %month_as_text%

EDIT:

Johannes suggests a shorthand for the 1st version:

set month_01=JAN
set month_02=FEB
set month_03=MAR
@rem ...

set number=02

setlocal enabledelayedexpansion

set month_as_text=!month_%number%!    

echo %month_as_text%
Frank Bollack
You can use delayed expansion to shorten array access to `!month_%number%!`.
Joey
@Johannes: I wasn't sure about that and couldn't find a quick reference. But you still have to define the array, thats why I'd prefer the second (shorter) solution. I'll add your sugestion anyway, thanks.
Frank Bollack
Yes, the second option is indeed nice :-) – sadly this only works for such a restricted set of items, since afaik the tokenizer only allows a maximum of 31 tokens.
Joey
Thanks for a great answer ! Helped me get one big step closer to my goal!
marc_s