views:

305

answers:

2

In my msbuild script I'm creating a zip file with year/month/day in the zip filename, but month and day are always written with no leading zero.

Is there a way to add leading zero to my zip filename?

<Time>
  <Output TaskParameter="Year" PropertyName="Year" />
  <Output TaskParameter="Month" PropertyName="Month" />
  <Output TaskParameter="Day" PropertyName="Day" />
</Time>

<PropertyGroup>
  <ZipOutDir>C:\output</ZipOutDir>
  <ZipFileName>Application_$(Year)$(Month)$(Day).zip</ZipFileName>
</PropertyGroup>

And the result is: 'Application_2010122.zip' (with no leading zero for january, as you can see)

A: 

You could use the MSBuild extension pack a la:

http://www.msbuildextensionpack.com/help/3.5.3.0/html/9c5401ed-6f55-089e-3918-2476c186ca66.htm

Or use the format param to the Time task from community tasks [which you appear to be using]

http://stackoverflow.com/questions/878037/msbuild-msbuildcommunitytasks-task-time

Ruben Bartelink
Thank you... I already found the later link and used it.
Goran
Good stuff. BTW Always a good idea to mention which task libs you're using / open to using when asking questions of this ilk.
Ruben Bartelink
A: 

It's because MSBuild operates solely with strings. You'll have to either modify existing tasks so that all properties will return strings instead of ints (or whatever integer value they return), or create a separate task which will format year, month and day according to your needs.

Anton Gogolev
Given the amount of task libs out there, just wanted to point out that your comment suggests writing a Task instead of finding one that already does it - I'm sure you didnt intend that. (And I'd use a PowerShell task to do tiny formatting hacks like this if I one of the common libs out there didnt already have a task for it)
Ruben Bartelink
@Ruben: Sure! I just couldn't find any string formatting task for MSBuild. Otherwise I'd definitely suggested using one of those.
Anton Gogolev
@Anton: Interesting that there isnt one actually... I'd be interested to know whether most people use a PowerShell task if cornered on something like this? (The best exanmple I can think of is that vdproj files need guids with capital letters in them and I resorted to PS to do a ToUpper as I couldnt find a Task to do it (or produce a captialized Guid))
Ruben Bartelink