![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) | ||||||||||||||||||||||||
| Guest | Re: String formatting of piped dates Try dir | foreach {$_.LastWritetime.Hour.tostring().padleft(2,"0")} Shay http://scriptolog.blogspot.com
PS>>
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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
PS>>>
| ||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 Specs![]() |
| | #7 (permalink) |
| 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 Specs![]() |
| | #8 (permalink) |
| 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 Specs![]() |
| | #9 (permalink) | ||||||||||||
| Guest | Re: String formatting of piped dates "Altraf" <Altraf@xxxxxx> wrote in message news:62AC7705-2226-41A2-8A7D-D43BEACB35B5@xxxxxx
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 Specs![]() | |||||||||||||
| | #10 (permalink) | ||||||||||||
| Guest | Re: String formatting of piped dates To add to the confusion: dir | foreach-object {"{0 2}" -f $_.LastWritetime.hour}"Altraf" wrote:
| ||||||||||||
My System Specs![]() | |||||||||||||
![]() |
| 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 |