13

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)

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Goran
  • 1,807
  • 7
  • 27
  • 41

4 Answers4

30

In msbuild 4 you can now

$([Namespace.Type]::Method(..parameters…))
$([Namespace.Type]::Property)
$([Namespace.Type]::set_Property(value))

so I am using

$([System.DateTime]::Now.ToString(`yyyy.MMdd`))

those ticks around the format are backticks not '

Bill Menees
  • 2,124
  • 24
  • 25
Maslow
  • 18,464
  • 20
  • 106
  • 193
4

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]

MSBuild MSBuildCommunityTasks Task Time

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 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 Jan 22 '10 at 12:01
  • It would be good if this accepted answer actually had the answer to the question added to it. That is, how, using the format param in the Time task from community tasks, does one format the date so there are leading 0s on the months? – ssmith Feb 01 '11 at 19:06
  • If you have the specific incantation to hand and you chat, gist or mail it to me, happy to paste it in. A the time of answering it, I personally didnt have a snippet to lift from a production system that I could be confident wouldnt be misleading or incorrect at the time of answering. (Saw your recent blog post so I'm sure you have one (sub'd and also see it via @CSharpFeeds)). Remember, I'm not @Sayed Ibrahim Hashimi that'll just roll up a full working tested example for amusement value :P – Ruben Bartelink Feb 03 '11 at 09:55
  • @ssmith: For some reason I omitted an @ssmith in my last response, Doh! – Ruben Bartelink Feb 06 '11 at 04:29
0

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
  • 113,561
  • 39
  • 200
  • 288
  • 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 Jan 22 '10 at 12:00
  • @Ruben: Sure! I just couldn't find any string formatting task for MSBuild. Otherwise I'd definitely suggested using one of those. – Anton Gogolev Jan 22 '10 at 12:03
  • 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 Jan 22 '10 at 12:15
-1

Here's a cheap and dirty way to add a leading zero

$([System.UInt16]::Parse($(Month)).ToString('00'))
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
DougP
  • 1
  • 1