This topic contains 6 replies, has 4 voices, and was last updated by  michael glenn 2 years, 11 months ago.

  • Author
  • #26761

    michael glenn

    im trying to do a "get-childitem" search of directories that are all named with numbers.
    a 5 digit minimum consisting of digits from 0 to 9. directory example names like the following "13476" or "334787"
    tried the below.. among many other attempts, and cant seem to get the results I'd like.

    get-childitem c:\users | where-object {$ -like '*0-9*' }

    how can I accomplish this?

  • #26762

    michael glenn

    ok.. I got this to return correctly.. is this the best way to accomplish it?
    basically.. I want it to return directory names that contain any integer of any length.

    get-childitem c:\users | where-object {$ -match '[0-9]' }
  • #26766

    Wilfredo Perez

    Try something like this

    Get-ChildItem -Path c:\ | Where-Object {$_.BaseName -match '(^\d[0-9])'}

    I did test and it only gave me the folders that start with numbers.

  • #26776

    michael glenn

    awesome.. thanks for the suggestion.

  • #26806

    Max Kozlov

    use \d+ or \d{min.max} syntax if you want to get 1+ or min to max numbers length. btw, \d its the same as [0-9]

    "^\d+$" – give names that contains numbers only
    "^\d{3,6}$" – give names that contains numbers only but only from 3 to 6 characters long
    "^\d{5,}$" gives what you want – all digits, five or more

  • #26825

    Rob Simmers

    You can actually filter with Get-ChildItem:

    Get-ChildItem -Path "C:\test\[0-9]*"

    It's referenced in this Powershell Tip:

  • #26958

    michael glenn

    thanks for the extra tips rob and max. its appreciated.
    now that ive got this aspect of the script working.. im running into the 256 character limit on directory length. always something.
    thanks again guys for all the help

You must be logged in to reply to this topic.