View Single Post
Old 12-18-2007   #2 (permalink)
Shay Levi


 
 

Re: Converting date time format to string format

Add ToString("f") on the foreach:

$created = get-date -year 2007 -month 12 -day 1
$files = get-childitem d:\backup | ? {!$_.PSIsContainer -and
($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
{$_.lastwriteTime.tostring("f")}


To make it clear

## $files contains array of datetime objects
$files = dir | select lastwritetime


## check each type of object in $files (not a string)
$files | foreach {$_.gettype()}

## now $files contains date strings
$files = dir | foreach {$_.lastwritetime.tostring("f")}


## check again each object in the array for its type, its all strings
$files | foreach {$_.gettype()}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> See Part 1 of this code
>
> ==================================================================
> # both are in date time format
> $created = get-date -year 2007 -month 12 -day 1
> $created1 = get-date
> # converted to string format
> $a = $created.tostring("f")
> $aa = $created1.tostring("f")
> # assign a variable to hold all values, separated by comma
> $grouparray = ($a,$aa)
> $group = $grouparray | foreach
> {([regex]'([A-za-z]+\s[0-9]+,\s[0-9]+)').match($_).value}
> $group | group -noelement | select name, count | sort -property name
> ==================================================================
> Result i got is :
> Name
> Count
> ----
> -----
> December 01, 2007
> 1
> December 18, 2007
> 1
> ==================================================================
> See Part 2 of this code
> ==================================================================
> # in date time format
> $created = get-date -year 2007 -month 12 -day 1
> $files = get-childitem d:\backup | ? {!$_.PSIsContainer -and
> ($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
> {$_.lastwriteTime}
> # still in date time format
> $files
> Results show below :
> ==================================================================
> Tuesday, December 04, 2007 12:34:55 PM
> Tuesday, December 04, 2007 12:09:04 PM
> Monday, December 10, 2007 1:57:47 PM
> Sunday, December 09, 2007 5:31:06 PM
> Tuesday, December 11, 2007 1:42:29 PM
> Tuesday, December 04, 2007 3:56:21 PM
> Wednesday, December 05, 2007 10:13:43 AM
> Tuesday, December 04, 2007 11:57:49 AM
> Tuesday, December 04, 2007 10:07:32 AM
> Friday, December 07, 2007 7:44:57 PM
> Monday, December 10, 2007 12:21:33 PM
> Tuesday, December 11, 2007 1:29:50 PM
> Monday, December 10, 2007 2:21:37 PM
> ==================================================================
> Here are the questions :
>
> a) In part 2 of the code, i want to convert $files into a string
> format. I try to do this :
>
> $abc = $files.tostring("f")
>
> This does not work. Error Cannot find an overload for "ToString" and
> the argument count: "1". I think the tostring method expects a "1"
> argument instead of multiple lines arguement. How do i convert $files
> into similar Part 1 of the code as in below :
>
> # assign a variable to hold all values, separated by comma
> # this is a string format i want.
> $grouparray = ($a,$aa)

My System SpecsSystem Spec