Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Add Date and time to filename

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-11-2007   #1 (permalink)
Jimbo
Guest


 

Add Date and time to filename

How do I add the date and time onto a filename I am generating in format
somefilename20070511-0911 ?

Thanks

My System SpecsSystem Spec
Old 05-11-2007   #2 (permalink)
Don Jones [MVP]
Guest


 

Re: Add Date and time to filename

if...
$d = get-date

then...
$d.year() + $d.month() + $d.day() + "-" $d.hour + $d.minute

Or something along those lines - use the methods of the datetime object to
get just the bits you want. You can also run $d.GetDateTimeFormats() to see
if one of the predefined string formats will work for you.

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Jimbo" <Jimbo@discussions.microsoft.com> wrote in message
news:333EB1A8-E981-4882-BA24-35D13000DA4E@microsoft.com...
> How do I add the date and time onto a filename I am generating in format
> somefilename20070511-0911 ?
>
> Thanks


My System SpecsSystem Spec
Old 05-11-2007   #3 (permalink)
Clint Bergman
Guest


 

Re: Add Date and time to filename

The more I play with the format operator ( -f ) the more I love it. You can generate your filename like this:

PS> $date = Get-Date
PS> $filename
PS> $filename = "FileName{0}{1:d2}{2:d2}-{3:d2}{4:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute
PS> $FileName
FileName20070511-1035

"get-help about_operators" has a little bit on the subject.

^_^ ~Clint

$filename = "FileName{0}{1:d2}{2:d2}-{3:d2}{4:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute
"Jimbo" <Jimbo@discussions.microsoft.com> wrote in message news:333EB1A8-E981-4882-BA24-35D13000DA4E@microsoft.com...
> How do I add the date and time onto a filename I am generating in format
> somefilename20070511-0911 ?
>
> Thanks



My System SpecsSystem Spec
Old 05-11-2007   #4 (permalink)
Maximilian Hänel
Guest


 

Re: Add Date and time to filename

Hi Clint,

> The more I play with the format operator ( -f ) the more I love it. You can generate your filename like this:
>
> PS> $date = Get-Date
> PS> $filename
> PS> $filename = "FileName{0}{1:d2}{2:d2}-{3:d2}{4:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute


Or just write it like that:

PS> $filename = "FileName{0:yyyyMMdd-HHmm}" -f (Get-Date)

hth

Max
My System SpecsSystem Spec
Old 05-11-2007   #5 (permalink)
Clint Bergman
Guest


 

Re: Add Date and time to filename

That is fantastic.

(^_^)

"Maximilian Hänel" <ngSpam@smjh.de> wrote in message news:eo8O3x$kHHA.4848@TK2MSFTNGP05.phx.gbl...
> Hi Clint,
>
>> The more I play with the format operator ( -f ) the more I love it. You can generate your filename like this:
>>
>> PS> $date = Get-Date
>> PS> $filename
>> PS> $filename = "FileName{0}{1:d2}{2:d2}-{3:d2}{4:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute

>
> Or just write it like that:
>
> PS> $filename = "FileName{0:yyyyMMdd-HHmm}" -f (Get-Date)
>
> hth
>
> Max



My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
date time formatting Andrew .NET General 4 07-09-2008 12:18 AM
Date time calculations Snowmizer PowerShell 11 04-18-2008 07:04 PM
Date and Time - Change Brink Tutorials 0 12-19-2007 03:28 PM
Date and Time inkd4life Vista General 6 10-07-2007 02:04 AM
Date and Time Brink Tutorials 0 06-24-2007 07:46 AM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008
Page generated in 0.26452 seconds with 10 queries