header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Group By Filename And Count Number of Occurances
Last Post 05 Sep 2010 12:28 PM by Rob Burgess. 11 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Rob BurgessUser is Offline
New Member
New Member
Posts:42
Avatar

--
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)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    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 BurgessUser is Offline
    New Member
    New Member
    Posts:42
    Avatar

    --
    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)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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 BurgessUser is Offline
    New Member
    New Member
    Posts:42
    Avatar

    --
    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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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 BurgessUser is Offline
    New Member
    New Member
    Posts:42
    Avatar

    --
    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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    04 Sep 2010 10:32 AM
    try this pattern:

    '^(.+@[^_]+)_.+$','$1'

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    Rob BurgessUser is Offline
    New Member
    New Member
    Posts:42
    Avatar

    --
    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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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 BurgessUser is Offline
    New Member
    New Member
    Posts:42
    Avatar

    --
    05 Sep 2010 12:28 PM
    Thanks Shay. That worked perfectly!

    Cheers
    Rob
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer