How to recursively append to file name in powershell?

前端 未结 2 521
小鲜肉
小鲜肉 2021-01-16 02:38

I have multiple .txt files in folders/their sub-folders.

I want to append _old to their file names.

I tried:

Get-ChildItem -Recurse | Rename-         


        
2条回答
  •  广开言路
    2021-01-16 03:27

    The below command will return ALL files from the current folder and sub-folders within the current directory the command is executed from.

    Get-ChildItem -Recurse
    

    Because of this you are also re-turning all the files you have already updated to have the _old suffix.

    What you need to do is use the -Include -Exclude paramters of the Get-Childitem Cmdlet in order to ignore files that already have the _old suffix, and meet your include criteria, for example.

    Get-ChildItem -Recure -Include "*.txt" -Exclude "*_old"
    

    Then pipe the results into your re-name item command

    Get-ChildItem cmdlet explanation can be found here.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7

提交回复
热议问题