Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
General PowerShell
Group By Filename And Count Number of Occurances
Last Post 05 Sep 2010 12:28 PM by
Rob Burgess
. 11 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
Rob Burgess
New Member
Posts:42
31 Aug 2010 07:53 PM
Hi
I need to create a script to count the number of occurances of files where the email address in the file name is the same.
Here is an example of a file name:
w_user@email.com_XID22Y42135F29r_Doc1.pdf
The email address is always between 'w_' and the second '_' in the filename.
What is the best way to do this?
Thanks
Rob
Marco Shaw (MVP)
Veteran Member
Posts:1641
31 Aug 2010 08:13 PM
Assuming your pattern remains the same:
PS>dir *.pdf|group {$_.name.split("_")[1]}
Marco
*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com
Rob Burgess
New Member
Posts:42
31 Aug 2010 08:31 PM
Thanks Marco. That worked great!
How would I deal with an email address in the filename that had an underscore (user_1@email.com). See example below:
w_user_1@email.com_jhfklhsf_Doc1.pdf
Cheers
Rob
Marco Shaw (MVP)
Veteran Member
Posts:1641
31 Aug 2010 09:34 PM
I hate regular expressions! That's going to be a bit more complicated, and I'm not able to figure it out right now.
Marco
*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
31 Aug 2010 11:28 PM
try this
dir -name | group {$_ -replace '^(.+@.+_.+)_.+$','$1'}
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
Rob Burgess
New Member
Posts:42
01 Sep 2010 08:16 PM
Thanks Shay
I tried your suggestion but I couldn't get it to group correctly. This is my script that Marco helped with:
dir "C:\Test" *.pdf|Where-Object {($_.lastwritetime -ge "08/01/2010" -and $_.lastwritetime -lt "08/25/2010")}|group {$_.name.split("_")[1]}
It works OK until the email address contains an underscore then it just groups by the characters between the first and second underscores. The example file below groups by 'user' instead of 'user_1@email.com'
w_user_1@email.com_jhfklhsf_Doc1.pdf
What is the best way to use you suggestion with the date filter I used above?
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
01 Sep 2010 11:57 PM
Does this work?
dir "C:\Test" *.pdf|Where-Object {($_.lastwritetime -ge "08/01/2010" -and $_.lastwritetime -lt "08/25/2010")} | group { $_.Name -replace '^(.+@.+_.+)_.+$','$1' }
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
Rob Burgess
New Member
Posts:42
03 Sep 2010 02:32 PM
Thanks Shay
The new code is grouping but not by the email address. The file name still includes the 'w_' at the front and '_' + characters to the right of the email address.
Heres an example:
Orginal file names
w_user@test.com_XID1au20v139Y6L_L62.pdf, w_user@test.com_XID1au20v139Y6L_L63.pdf, w_user@test.com_XID1au20v139Y6L_L64.pdf
Result
Name Count
--------- ----------
w_user@test.com_XID1au20v139Y6L 3
Rob
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
04 Sep 2010 10:32 AM
try this pattern:
'^(.+@[^_]+)_.+$','$1'
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
Rob Burgess
New Member
Posts:42
04 Sep 2010 02:27 PM
Thanks for your help Shay. The grouping and counting seems to be working ok now. Could you please tell me how would I would remove the 'w_' from the front of the Name object that is returned?
Rob
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
05 Sep 2010 12:30 AM
Change the regex pattern to the following:
PS > 'w_user@test.com_XID1au20v139Y6L_L64.pdf' -replace '^w_(.+@[^_]+)_.+$','$1'
user@test.com
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
Rob Burgess
New Member
Posts:42
05 Sep 2010 12:28 PM
Thanks Shay. That worked perfectly!
Cheers
Rob
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2