1

I'm trying to write a powershell script to rename several files. Their file names are originally long alphanumeric strings that I'm hoping to rename to something more generic and add number to the end. Something like file1, file2, file3, etc. I'm using the Get-ChildItem and Rename-Item cmdlets but can't figure out how to add the increasing number to the end of the file name. I don't know how many files will need to be renamed every time, it could be one or two or it could be thousands. Can anyone point me in the right direction?

$NameIncrease = 1
Get-ChildItem -Path C:\Folder *.dcm -Recurse | 
  % { rename-item -NewName ('File {0} - {1}' -f $NameIncrease++) }
mklement0
  • 382,024
  • 64
  • 607
  • 775
locutus
  • 11
  • 1
  • 1
    you have TWO placeholders in your format pattern ... but only ONE value. otherwise, your code looks like it otta work. **_what is not working as needed?_** – Lee_Dailey Nov 09 '21 at 01:53
  • Thank you @mklement0 I got the renaming working by modifying the code in the linked question. However, it does seem like it's getting stuck recursing and renaming the files over and over. Is there a way to have it stop after it's renamed each file once? The path the command is calling is Folder > Child Directory > File. I am providing this path since the child directories are renamed by an earlier cmdlet and I'm not sure how many children directories there are. `Get-ChildItem -Path c:\folder *.dcm -Rcurse | Rename-Item -NewName {('Image{0}.dcm' -f (Get-Variable ImageIncrease).Value++)}` – locutus Nov 09 '21 at 22:04
  • @locutus, enclose `Get-ChildItem -Path C:\Folder *.dcm -Recurse` in `(...)` to force collecting its output _up front_, to prevent potential problems with already renamed file re-entering the file-system enumeration. If this doesn't help, please create a _new_ question post, focused just on your remaining problem. – mklement0 Nov 09 '21 at 22:10

0 Answers0