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 - Group by daily hour / per day

Reply
 
Old 12-19-2007   #1 (permalink)
IT Staff


 
 

Group by daily hour / per day

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

# sort by string-date "g" format
$group = $files | foreach
{([regex]'([0-9]+/[0-9]+/[0-9]+\s[0-9]+:[0-9]+\s+[A-z]+[A-z])').match($_).value}

#display to screen in group
$group | group -noelement | select name, count | sort -property name |
ft -auto

===============================================================================
Sample results below :. See questions right below

12/17/2007 9:06 PM 2
12/17/2007 9:09 AM 2
12/17/2007 9:10 AM 14
12/17/2007 9:12 AM 2
12/17/2007 9:13 AM 1
12/17/2007 9:15 AM 1
12/17/2007 9:16 AM 1
12/17/2007 9:17 AM 1
12/17/2007 9:18 AM 2
12/17/2007 9:19 AM 3
12/17/2007 9:20 AM 6
12/17/2007 9:22 AM 1
12/17/2007 9:24 AM 1
12/17/2007 9:25 AM 6
12/17/2007 9:27 AM 2
12/17/2007 9:28 AM 3
12/17/2007 9:29 AM 14
12/17/2007 9:30 AM 5
12/17/2007 9:31 AM 5
12/17/2007 9:32 AM 13
12/17/2007 9:33 AM 3
12/17/2007 9:35 AM 6
12/17/2007 9:36 AM 2
12/17/2007 9:38 AM 9
12/17/2007 9:39 AM 2
12/17/2007 9:40 AM 5
12/17/2007 9:41 AM 1
12/17/2007 9:43 AM 1
12/17/2007 9:45 AM 3
12/17/2007 9:46 AM 1
12/17/2007 9:48 AM 4
12/17/2007 9:49 AM 2
12/17/2007 9:50 AM 3
12/17/2007 9:53 AM 1
12/17/2007 9:55 AM 5
12/17/2007 9:56 AM 30
12/17/2007 9:57 AM 3
12/17/2007 9:58 AM 3
12/18/2007 10:00 AM 5
12/18/2007 10:01 AM 5
12/18/2007 10:02 AM 1
12/18/2007 10:03 AM 1
12/18/2007 10:04 AM 1
12/18/2007 10:05 AM 42
12/18/2007 10:06 AM 1
12/18/2007 10:09 AM 1
12/18/2007 10:10 AM 2
12/18/2007 10:11 AM 1
12/18/2007 10:12 AM 3
12/18/2007 10:15 AM 8
12/18/2007 10:17 AM 3
12/18/2007 10:18 AM 8
12/18/2007 10:19 AM 7
12/18/2007 10:20 AM 8
===============================================================================

Questions :

a) I want to group by the per day / hour. Eg

12/17/2007 (9:01 am to 10am) = xx
12/17/2007 (10:01am to 11am) = xx
etc etc
12/18/2007 (9:01 am to 10am) = xx
12/18/2007 (10:01 am to 10am) = xx







My System SpecsSystem Spec
Old 12-19-2007   #2 (permalink)
Shay Levi


 
 

Re: Group by daily hour / per day

Why dont ya group the dates while they're still an object and not a string,
this way you can group them
by any datetime property you like, you can convert to strings later on:

$created = get-date -year 2007 -month 12 -day 1
$files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
($_.lastwriteTime -ge $created)}


$groupByDay = $created | group {$_.lastwriteTime.Day}
$groupByHour = $created | group {$_.lastwriteTime.Hour}


## convert to strings
$dateStrings = $created | foreach {$_.lastwriteTime.tostring("g")}


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


Quote:

> $created = get-date -year 2007 -month 12 -day 1
> $files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
> ($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
> {$_.lastwriteTime.tostring("g")}
> # sort by string-date "g" format
> $group = $files | foreach
> {([regex]'([0-9]+/[0-9]+/[0-9]+\s[0-9]+:[0-9]+\s+[A-z]+[A-z])').match(
> $_).value}
> #display to screen in group
> $group | group -noelement | select name, count | sort -property name |
> ft -auto
> ======================================================================
> ========= Sample results below :. See questions right below
>
> 12/17/2007 9:06 PM 2
> 12/17/2007 9:09 AM 2
> 12/17/2007 9:10 AM 14
> 12/17/2007 9:12 AM 2
> 12/17/2007 9:13 AM 1
> 12/17/2007 9:15 AM 1
> 12/17/2007 9:16 AM 1
> 12/17/2007 9:17 AM 1
> 12/17/2007 9:18 AM 2
> 12/17/2007 9:19 AM 3
> 12/17/2007 9:20 AM 6
> 12/17/2007 9:22 AM 1
> 12/17/2007 9:24 AM 1
> 12/17/2007 9:25 AM 6
> 12/17/2007 9:27 AM 2
> 12/17/2007 9:28 AM 3
> 12/17/2007 9:29 AM 14
> 12/17/2007 9:30 AM 5
> 12/17/2007 9:31 AM 5
> 12/17/2007 9:32 AM 13
> 12/17/2007 9:33 AM 3
> 12/17/2007 9:35 AM 6
> 12/17/2007 9:36 AM 2
> 12/17/2007 9:38 AM 9
> 12/17/2007 9:39 AM 2
> 12/17/2007 9:40 AM 5
> 12/17/2007 9:41 AM 1
> 12/17/2007 9:43 AM 1
> 12/17/2007 9:45 AM 3
> 12/17/2007 9:46 AM 1
> 12/17/2007 9:48 AM 4
> 12/17/2007 9:49 AM 2
> 12/17/2007 9:50 AM 3
> 12/17/2007 9:53 AM 1
> 12/17/2007 9:55 AM 5
> 12/17/2007 9:56 AM 30
> 12/17/2007 9:57 AM 3
> 12/17/2007 9:58 AM 3
> 12/18/2007 10:00 AM 5
> 12/18/2007 10:01 AM 5
> 12/18/2007 10:02 AM 1
> 12/18/2007 10:03 AM 1
> 12/18/2007 10:04 AM 1
> 12/18/2007 10:05 AM 42
> 12/18/2007 10:06 AM 1
> 12/18/2007 10:09 AM 1
> 12/18/2007 10:10 AM 2
> 12/18/2007 10:11 AM 1
> 12/18/2007 10:12 AM 3
> 12/18/2007 10:15 AM 8
> 12/18/2007 10:17 AM 3
> 12/18/2007 10:18 AM 8
> 12/18/2007 10:19 AM 7
> 12/18/2007 10:20 AM 8
> ======================================================================
> =========
> Questions :
>
> a) I want to group by the per day / hour. Eg
>
> 12/17/2007 (9:01 am to 10am) = xx
> 12/17/2007 (10:01am to 11am) = xx
> etc etc
> 12/18/2007 (9:01 am to 10am) = xx
> 12/18/2007 (10:01 am to 10am) = xx

My System SpecsSystem Spec
Old 12-19-2007   #3 (permalink)
Steven Hystad


 
 

Re: Group by daily hour / per day

First I have to advise that try not to apply any formatting to data before
you have the data that you want to display.
What you want to display is the group name for each hour and the number of
files that were last written to in that hour.
When time is involved it is easiest to work with the DateTime objects rather
than working with strings that represent those times.

Just now I used the following code to group files by month according the
LastWriteTime:
gci | group {New-Object DateTime ($_.LastWriteTime.Year,
$_.LastWriteTime.Month, 1)}

When replacing gci with the code you used to get your file list and
increasing accuracy to one hour I get the following code:
$created = get-date -year 2007 -month 12 -day 1
$files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
($_.lastwriteTime -ge $created)}
$files | group {New-Object DateTime ($_.LastWriteTime.Year,
$_.LastWriteTime.Month, $_.LastWriteTime.Day, $_.LastWriteTime.Hour, 0, 0)}

As for formatting that's all about personal taste so you'll have to figure
that one out on your own.

"IT Staff" <jkklim@xxxxxx> wrote in message
news:Ord2fjgQIHA.3940@xxxxxx
Quote:

> $created = get-date -year 2007 -month 12 -day 1
> $files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
> ($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
> {$_.lastwriteTime.tostring("g")}
>
> # sort by string-date "g" format
> $group = $files | foreach
> {([regex]'([0-9]+/[0-9]+/[0-9]+\s[0-9]+:[0-9]+\s+[A-z]+[A-z])').match($_).value}
>
> #display to screen in group
> $group | group -noelement | select name, count | sort -property name |
> ft -auto
>
> ===============================================================================
> Sample results below :. See questions right below
>
> 12/17/2007 9:06 PM 2
> 12/17/2007 9:09 AM 2
> 12/17/2007 9:10 AM 14
> 12/17/2007 9:12 AM 2
> 12/17/2007 9:13 AM 1
> 12/17/2007 9:15 AM 1
> 12/17/2007 9:16 AM 1
> 12/17/2007 9:17 AM 1
> 12/17/2007 9:18 AM 2
> 12/17/2007 9:19 AM 3
> 12/17/2007 9:20 AM 6
> 12/17/2007 9:22 AM 1
> 12/17/2007 9:24 AM 1
> 12/17/2007 9:25 AM 6
> 12/17/2007 9:27 AM 2
> 12/17/2007 9:28 AM 3
> 12/17/2007 9:29 AM 14
> 12/17/2007 9:30 AM 5
> 12/17/2007 9:31 AM 5
> 12/17/2007 9:32 AM 13
> 12/17/2007 9:33 AM 3
> 12/17/2007 9:35 AM 6
> 12/17/2007 9:36 AM 2
> 12/17/2007 9:38 AM 9
> 12/17/2007 9:39 AM 2
> 12/17/2007 9:40 AM 5
> 12/17/2007 9:41 AM 1
> 12/17/2007 9:43 AM 1
> 12/17/2007 9:45 AM 3
> 12/17/2007 9:46 AM 1
> 12/17/2007 9:48 AM 4
> 12/17/2007 9:49 AM 2
> 12/17/2007 9:50 AM 3
> 12/17/2007 9:53 AM 1
> 12/17/2007 9:55 AM 5
> 12/17/2007 9:56 AM 30
> 12/17/2007 9:57 AM 3
> 12/17/2007 9:58 AM 3
> 12/18/2007 10:00 AM 5
> 12/18/2007 10:01 AM 5
> 12/18/2007 10:02 AM 1
> 12/18/2007 10:03 AM 1
> 12/18/2007 10:04 AM 1
> 12/18/2007 10:05 AM 42
> 12/18/2007 10:06 AM 1
> 12/18/2007 10:09 AM 1
> 12/18/2007 10:10 AM 2
> 12/18/2007 10:11 AM 1
> 12/18/2007 10:12 AM 3
> 12/18/2007 10:15 AM 8
> 12/18/2007 10:17 AM 3
> 12/18/2007 10:18 AM 8
> 12/18/2007 10:19 AM 7
> 12/18/2007 10:20 AM 8
> ===============================================================================
>
> Questions :
>
> a) I want to group by the per day / hour. Eg
>
> 12/17/2007 (9:01 am to 10am) = xx
> 12/17/2007 (10:01am to 11am) = xx
> etc etc
> 12/18/2007 (9:01 am to 10am) = xx
> 12/18/2007 (10:01 am to 10am) = xx
>
>
>
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #4 (permalink)
Kiron


 
 

Re: Group by daily hour / per day

Try this:

$created = get-date -year 2007 -month 12 -day 1
ls d:\fac\queue | ? {!$_.psIsContainer -and
$_.lastwriteTime -ge $created} | sort LastWriteTime |
group {$_.LastWriteTime.toString("yyyyMMddHH")} |
select @{n='Date'; e={$_.name -replace `
'^(....)(..)(..)(..)$', '$2/$3/$1 $4 hours'}}, Count |
ft -a

--
Kiron
My System SpecsSystem Spec
Old 12-24-2007   #5 (permalink)
IT Staff


 
 

Re: Group by daily hour / per day

Thks steven, this works well.

However can you explain the last 0,0 parameters ? Is there a url i can read
on ?


{New-Object DateTime ($_.LastWriteTime.Year, $_.LastWriteTime.Month,
$_.LastWriteTime.Day, $_.LastWriteTime.Hour, 0, 0)}




"Steven Hystad" <steven.hystad@xxxxxx> wrote in message
news:%23W3w%23chQIHA.5164@xxxxxx
Quote:

> First I have to advise that try not to apply any formatting to data before
> you have the data that you want to display.
> What you want to display is the group name for each hour and the number of
> files that were last written to in that hour.
> When time is involved it is easiest to work with the DateTime objects
> rather than working with strings that represent those times.
>
> Just now I used the following code to group files by month according the
> LastWriteTime:
> gci | group {New-Object DateTime ($_.LastWriteTime.Year,
> $_.LastWriteTime.Month, 1)}
>
> When replacing gci with the code you used to get your file list and
> increasing accuracy to one hour I get the following code:
> $created = get-date -year 2007 -month 12 -day 1
> $files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
> ($_.lastwriteTime -ge $created)}
> $files | group {New-Object DateTime ($_.LastWriteTime.Year,
> $_.LastWriteTime.Month, $_.LastWriteTime.Day, $_.LastWriteTime.Hour, 0,
> 0)}
>
> As for formatting that's all about personal taste so you'll have to figure
> that one out on your own.
>
> "IT Staff" <jkklim@xxxxxx> wrote in message
> news:Ord2fjgQIHA.3940@xxxxxx
Quote:

>> $created = get-date -year 2007 -month 12 -day 1
>> $files = get-childitem d:\fac\queue | ? {!$_.PSIsContainer -and
>> ($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
>> {$_.lastwriteTime.tostring("g")}
>>
>> # sort by string-date "g" format
>> $group = $files | foreach
>> {([regex]'([0-9]+/[0-9]+/[0-9]+\s[0-9]+:[0-9]+\s+[A-z]+[A-z])').match($_).value}
>>
>> #display to screen in group
>> $group | group -noelement | select name, count | sort -property name |
>> ft -auto
>>
>> ===============================================================================
>> Sample results below :. See questions right below
>>
>> 12/17/2007 9:06 PM 2
>> 12/17/2007 9:09 AM 2
>> 12/17/2007 9:10 AM 14
>> 12/17/2007 9:12 AM 2
>> 12/17/2007 9:13 AM 1
>> 12/17/2007 9:15 AM 1
>> 12/17/2007 9:16 AM 1
>> 12/17/2007 9:17 AM 1
>> 12/17/2007 9:18 AM 2
>> 12/17/2007 9:19 AM 3
>> 12/17/2007 9:20 AM 6
>> 12/17/2007 9:22 AM 1
>> 12/17/2007 9:24 AM 1
>> 12/17/2007 9:25 AM 6
>> 12/17/2007 9:27 AM 2
>> 12/17/2007 9:28 AM 3
>> 12/17/2007 9:29 AM 14
>> 12/17/2007 9:30 AM 5
>> 12/17/2007 9:31 AM 5
>> 12/17/2007 9:32 AM 13
>> 12/17/2007 9:33 AM 3
>> 12/17/2007 9:35 AM 6
>> 12/17/2007 9:36 AM 2
>> 12/17/2007 9:38 AM 9
>> 12/17/2007 9:39 AM 2
>> 12/17/2007 9:40 AM 5
>> 12/17/2007 9:41 AM 1
>> 12/17/2007 9:43 AM 1
>> 12/17/2007 9:45 AM 3
>> 12/17/2007 9:46 AM 1
>> 12/17/2007 9:48 AM 4
>> 12/17/2007 9:49 AM 2
>> 12/17/2007 9:50 AM 3
>> 12/17/2007 9:53 AM 1
>> 12/17/2007 9:55 AM 5
>> 12/17/2007 9:56 AM 30
>> 12/17/2007 9:57 AM 3
>> 12/17/2007 9:58 AM 3
>> 12/18/2007 10:00 AM 5
>> 12/18/2007 10:01 AM 5
>> 12/18/2007 10:02 AM 1
>> 12/18/2007 10:03 AM 1
>> 12/18/2007 10:04 AM 1
>> 12/18/2007 10:05 AM 42
>> 12/18/2007 10:06 AM 1
>> 12/18/2007 10:09 AM 1
>> 12/18/2007 10:10 AM 2
>> 12/18/2007 10:11 AM 1
>> 12/18/2007 10:12 AM 3
>> 12/18/2007 10:15 AM 8
>> 12/18/2007 10:17 AM 3
>> 12/18/2007 10:18 AM 8
>> 12/18/2007 10:19 AM 7
>> 12/18/2007 10:20 AM 8
>> ===============================================================================
>>
>> Questions :
>>
>> a) I want to group by the per day / hour. Eg
>>
>> 12/17/2007 (9:01 am to 10am) = xx
>> 12/17/2007 (10:01am to 11am) = xx
>> etc etc
>> 12/18/2007 (9:01 am to 10am) = xx
>> 12/18/2007 (10:01 am to 10am) = xx
>>
>>
>>
>>
>>
>>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
add user to group /group scope - Global /Group type - Security PowerShell
Daily crash on Vista SP1 Vista General
WGA on a daily basis Vista General
Daily Error Vista General
Is there a way to shut down Windows daily at, say 1 am? Vista General


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