none
Powershell Count substringed file names RRS feed

  • Question

  • I need to SubString the first 7 numbers from file names in a directory then count the occurances of all duplicate SubStrings.

    Get-ChildItem 'C:\Temp' | %{ $id = $_.Name.substring(0,7) $Selection = ($id | Select-String $id -AllMatches) $Selection.Matches.Count }

    SubString looks good but no counting, I get a 0 for each file in the dir.

    Any ideas

    Friday, January 16, 2015 4:00 AM

Answers

  • Why?  Just count the names?

    dir * |%{$_.Name.SubString(0,7)} | group |select name,count|ft -auto

    Why is this a problem?


    ¯\_(ツ)_/¯

    • Marked as answer by rio228 Friday, January 16, 2015 7:20 AM
    Friday, January 16, 2015 6:19 AM

All replies

  • None of thismakes any sense.  Perhaps you could clarify what you are asing.

    ¯\_(ツ)_/¯

    Friday, January 16, 2015 5:15 AM
  • In a folder I have files with 7digits at the start of the name,

    1234567_rest_of _the_file.pic.

    1234567_hello_world.pic, etc.

    I would like to write to a text file the matching file names with a count of how many have the same 7 digits.

    Any sort of grouping or matching that I try only returns a count of 1 or 0. If i write-host the substring out it looks fine. It seems like the grouping is being done on the full file name and not the substring output.

    I am hoping for

    File               Count

    1234567        2

    I hope that this makes it clearer.

    Thanks

    Friday, January 16, 2015 5:41 AM
  • Why?  Just count the names?

    dir * |%{$_.Name.SubString(0,7)} | group |select name,count|ft -auto

    Why is this a problem?


    ¯\_(ツ)_/¯

    • Marked as answer by rio228 Friday, January 16, 2015 7:20 AM
    Friday, January 16, 2015 6:19 AM
  • So Simple, just tried that at home on some test files and yes that's exactly what I need.

    Thank so much!!!

    Friday, January 16, 2015 7:20 AM