How to target files in a certain sub dir with get-childitem

  • Hi All,

    So I have a list of directories:

    D:\Data\Load\customer1\Archive

    D:\Data\Load\customer2\Archive

    D:\Data\Load\customer3\Archive

    D:\Data\Load\customer4\Archive

    We receive files with spaces and i want to replace those spaces with dashes, but i want to only target items in the archive directories.

    Im using this but cant find the command to make it only look files in the archive folder

    Get-ChildItem D:\Data\Load -Filter "*.zip" -Recurse | Rename-Item -NewName {$_.name -replace ' ','-' } -whatif

    this works but will also touch files in the root of the customers folder, which i would prefer not to happen.

    Thanks in advance!

  • jzieleniuk (5/19/2015)


    We receive files with spaces and i want to replace those spaces with dashes...

    Why? There should be no problem with handling files with spaces. You just need to encapsulate them with double-quotes in whatever you're doing.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • jzieleniuk (5/19/2015)


    Hi All,

    So I have a list of directories:

    D:\Data\Load\customer1\Archive

    D:\Data\Load\customer2\Archive

    D:\Data\Load\customer3\Archive

    D:\Data\Load\customer4\Archive

    We receive files with spaces and i want to replace those spaces with dashes, but i want to only target items in the archive directories.

    Im using this but cant find the command to make it only look files in the archive folder

    Get-ChildItem D:\Data\Load -Filter "*.zip" -Recurse | Rename-Item -NewName {$_.name -replace ' ','-' } -whatif

    this works but will also touch files in the root of the customers folder, which i would prefer not to happen.

    Thanks in advance!

    You could do this:

    Get-ChildItem D:\Data\Load\*\Archive -Filter "*.zip" | Rename-Item -NewName {$_.name -replace ' ','-' } -whatif

    This assumes that you don't have any other Archive folders with a similar \data\load\*\ path.

    -Recurse is making the PS script look in every directory in the path. You don't need it to just look in the Archive directory.

  • This is just a clean up step I want to do. Previously whoever archived these files would put a space between the file and the datetime stamp when they manually archive and renamed the files. I have since automated the procedure, and had a lot of old files that i just wanted to get in a standardized format.

  • Normally you can tell PowerShell if you want to include directories or not.

    Ex:

    Get-ChildItem D:\Data\Load -Filter "*.zip" -Recurse | where {$_.psiscontainer -eq $false} | Rename-Item -NewName {$_.name -replace ' ','-' } -whatif

    Joie Andrew
    "Since 1982"

  • Get-ChildItem D:\Data\Load -Directory | Get-ChildItem -Filter "*.zip" | Rename-Item -NewName {$_.name -replace ' ','-' } -whatif

    Get the folders, then within each folder get the ZIP files and for each ZIP file rename it.

    Think of each logical step and DON'T combine them to avoid side effects!!!

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply