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

String formatting of piped dates

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-09-2007   #1 (permalink)
Altraf
Guest


 

String formatting of piped dates

I want to get a dir then select on the LastWriteTime and from that get the
hour formatted with a leading zero if less than 10.

This is what I tried but it failed:

PS > dir | select "{0}" -f {$_.LastWritetime.hour}
Select-Object : Cannot bind parameter 'First'. Cannot convert
"$_.LastWritetime.hour" to "System.Int32".
At line:1 char:23
+ dir | select "{0}" -f <<<< {$_.LastWritetime.hour}
PS >

I'm new to Powershell so any assistance would be appreciated.

Thanks

Altraf

My System SpecsSystem Spec
Old 10-09-2007   #2 (permalink)
Kiron
Guest


 

Re: String formatting of piped dates

Using calculated property to create the version of LastWriteTime you want:

dir | select Mode, @{name = 'LastWriteTime';
expression = {"{0:mm/dd/yyyy hh:MM tt}" -f ($_.lastWriteTime)}},
Name, Length | format-table -auto

--
Kiron
My System SpecsSystem Spec
Old 10-09-2007   #3 (permalink)
Shay Levi
Guest


 

Re: String formatting of piped dates


Try

dir | foreach {$_.LastWritetime.Hour.tostring().padleft(2,"0")}



Shay
http://scriptolog.blogspot.com


Quote:

> I want to get a dir then select on the LastWriteTime and from that get
> the hour formatted with a leading zero if less than 10.
>
> This is what I tried but it failed:
>
PS>> dir | select "{0}" -f {$_.LastWritetime.hour}
PS>>
Quote:

> Select-Object : Cannot bind parameter 'First'. Cannot convert
> "$_.LastWritetime.hour" to "System.Int32".
> At line:1 char:23
> + dir | select "{0}" -f <<<< {$_.LastWritetime.hour}
> I'm new to Powershell so any assistance would be appreciated.
>
> Thanks
>
> Altraf
>

My System SpecsSystem Spec
Old 10-09-2007   #4 (permalink)
Shay Levi
Guest


 

Re: String formatting of piped dates


With calculated property

dir | select name,@{name="Hour";expression={$_.LastWritetime.hour.tostring().padleft(2,"0")}}



Shay
http://scriptolog.blogspot.com


Quote:

> Try
>
> dir | foreach {$_.LastWritetime.Hour.tostring().padleft(2,"0")}
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> I want to get a dir then select on the LastWriteTime and from that
>> get the hour formatted with a leading zero if less than 10.
>>
>> This is what I tried but it failed:
>>
PS>>> dir | select "{0}" -f {$_.LastWritetime.hour}
PS>>>
Quote:
Quote:

>> Select-Object : Cannot bind parameter 'First'. Cannot convert
>> "$_.LastWritetime.hour" to "System.Int32".
>> At line:1 char:23
>> + dir | select "{0}" -f <<<< {$_.LastWritetime.hour}
>> I'm new to Powershell so any assistance would be appreciated.
>> Thanks
>>
>> Altraf
>>

My System SpecsSystem Spec
Old 10-09-2007   #5 (permalink)
Kiron
Guest


 

Re: String formatting of piped dates

Sorry first post had a typo:

# Using select-object:

dir | select Mode, @{name = 'LastWriteTime';
expression = {"{0:MM/dd/yyyy hh:mm tt}" -f ($_.lastWriteTime)}},
Name, Length | format-table -auto

# Using formatt-object, its calculated field has an alignment attribute:

dir | format-table Mode, @{label = 'LastWriteTime';
expression = {"{0:MM/dd/yyyy hh:mm tt}" -f ($_.lastWriteTime)}},
@{label = 'Length';
expression = {$_.length}; align = 'Right'}, Name -auto

--
Kiron
My System SpecsSystem Spec
Old 10-09-2007   #6 (permalink)
Altraf
Guest


 

Re: String formatting of piped dates

Thanks Kiron

After looking at your example I ended up with:

dir | select @{name="LastWriteTime";expression={"{0:hh}" -f $_.lastWriteTime}}

This does what I want, and I've learnt a bit more about Powershell.

Altraf
My System SpecsSystem Spec
Old 10-09-2007   #7 (permalink)
Altraf
Guest


 

Re: String formatting of piped dates

Hi Shay Levi

Your suggestion also gives the desired result. I now have two ways of
achieving my goal.

Thanks.

Alan
My System SpecsSystem Spec
Old 10-09-2007   #8 (permalink)
Brandon Shell [MVP]
Guest


 

Re: String formatting of piped dates

Oh the glory of powershell

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

A> Hi Shay Levi
A>
A> Your suggestion also gives the desired result. I now have two ways
A> of achieving my goal.
A>
A> Thanks.
A>
A> Alan
A>


My System SpecsSystem Spec
Old 10-09-2007   #9 (permalink)
Jon
Guest


 

Re: String formatting of piped dates


"Altraf" <Altraf@xxxxxx> wrote in message
news:62AC7705-2226-41A2-8A7D-D43BEACB35B5@xxxxxx
Quote:

> Thanks Kiron
>
> After looking at your example I ended up with:
>
> dir | select @{name="LastWriteTime";expression={"{0:hh}" -f
> $_.lastWriteTime}}
>
> This does what I want, and I've learnt a bit more about Powershell.
>
> Altraf

Be aware that times after midday will be displayed in 12 hour format with
that, which may not be what you want.


Compare to (24 hour format)

dir | select @{name="LastWriteTime";expression={"{0:HH}" -f
$_.lastWriteTime}}


There's a good discussion here on formatting dates and times in PowerShell,
which may also give you some more ideas

Formatting Dates and Times
http://www.microsoft.com/technet/scr...pstip0831.mspx



--
Jon


My System SpecsSystem Spec
Old 10-09-2007   #10 (permalink)
Rob Campbell
Guest


 

Re: String formatting of piped dates

To add to the confusion:

dir | foreach-object {"{02}" -f $_.LastWritetime.hour}

"Altraf" wrote:
Quote:

> Hi Shay Levi
>
> Your suggestion also gives the desired result. I now have two ways of
> achieving my goal.
>
> Thanks.
>
> Alan
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with Piped output NPopovich PowerShell 3 08-28-2008 03:21 PM
Using $matches in string formatting via -f Altraf PowerShell 5 10-25-2007 01:50 PM
Formatting objects in a string olig PowerShell 3 08-07-2007 07:38 AM
Any limit to the size of a .NET object that can be piped from one cmdlet to another? Michael Herman \(Parallelspace/OpenCanal\) PowerShell 5 04-09-2007 03:53 PM
Import-CSV piped hashtable access Terry E Dow PowerShell 2 12-29-2006 07:44 AM


Update your Vista Drivers Update Your 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