Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Add Date and time to filename

Reply
 
Old 05-11-2007   #1 (permalink)
Jimbo


 
 

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]


 
 

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


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
ren filename with time help PowerShell
get-date - make it a filename PowerShell
Date and Time slowing down Vista performance & maintenance
Date and Time Vista General
Date and Time Tutorials


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46