views:

114

answers:

2

I have a folder with 2000+ files. I want to count the files by date.

so with:

  Mode                LastWriteTime     Length Name  
   ----                -------------     ------ ----  
   -a---        2010-03-15  12:54 AM   10364953 file1.txt  
   -a---        2010-03-15   1:07 AM   10650503 file2.txt  
   -a---        2010-03-16   1:20 AM   10118657 file3.txt  
   -a---        2010-03-16   1:33 AM    9735542 file4.txt  
   -a---        2010-03-18   1:46 AM   10666979 file5.txt  

I'd like to see:

Date         Count
----------   ------
2010-03-15   2  
2010-03-16   2  
2010-03-18   1

Thanks!

+1  A: 

You can use a hash table to collect the information you need gci is an alias from get-childitem

 $dict = new-object -t system.collections.hashtable
 gci * |? { $dict[$_.lastwritetime.date]++ }
 $dict
rerun
Awesome, that did the trick!
Shane Cusson
+2  A: 

Group-Object can handle this sort of chore pretty easily:

Get-ChildItem | Group {$_.LastWriteTime.ToString("yyyy-MM-dd")} | Sort Name

If you only want to see the date and count tack on the Format-Table as shown below:

Get-ChildItem | Group {$_.LastWriteTime.ToString("yyyy-MM-dd")} | Sort Name | 
    Format-Table Name,Count -auto
Keith Hill
Ah, a one-liner, even better.
Shane Cusson
Side Note: `group { $_.LastWriteTime.Date }` works as well but returns the timestamp in the `Name` property as well (though set to `0:00:00`). That's likely why Keith uses a formatted string here instead (which confused me initially). Another option would be `Format-Table -GroupBy` which has a different output.
Joey
Plus the format string gets the date in the format Shane wanted. :-). Note that you have to be careful using Format-Table -GroupBy. If you use it, be sure to sort first on what you are grouping by otherwise you can wind up with the same "group" specified multiple times.
Keith Hill