0

How can I rename files in a folder that have random names and random extensions to a sequence like the example below: 0001.pdf 0002.pdf ..... 0100.png and continue.

And if possible then generate a .txt file with the names and extensions generated.

For the .txt file if not possible Powershel could be another application.

Searching I got the code below, but I can't fix it for the task I need.

Dir | Rename-Item –NewName { $_.name –replace " - ","0" }

1
  • 2
    [ref]$i = 1; Get-ChildItem | Rename-Item -NewName { "{0}{1}"-f $i.Value++, $_.Extension } should work as well. Commented Aug 26, 2022 at 13:47

1 Answer 1

1

Wrap the call to Rename-Item in ForEach-Object then maintain a counter in a variable:

$fileNumber = 1 Get-ChildItem path\to\folder\containing\random\files -File |ForEach-Object { # Construct new file name $newName = '{0:0000}{1}' -f $fileNumber,$_.Extension # Perform rename $_ |Rename-Item -NewName $newName # Increment number $fileNumber++ } 
Sign up to request clarification or add additional context in comments.

2 Comments

@mathias-r-jessen Worked perfectly. Is there any application or even powershell that takes these generated files and makes a list in a .txt file with the generated names?
@kasten you mean after the fact? You can do that fairly easily with Get-ChildItem path\to\folder\containing\random\files -File |Select -Expand Name |Out-File path\to\inventory.txt

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.