replace/rename 1300 .png files at once, cmd or powershell? replace the last 4 digits of ever name

437 views Asked by At

im a noob to the cmd and windows powershell, i do not want to mess anything up so im asking smarter people...

this was my reference https://www.thomas-krenn.com/en/wiki/Cmd_commands_under_Windows

i tried: find *.png -exec mv {} ${foo: -4} ; but i got this "find: parameter format not correct"

can anyone tell me how i messed up?

im trying to remove the last 4 digits of every .png file in a folder. to be clear, im not trying to overrite the .png extension. only the last 4 digits of the name

3

There are 3 answers

2
Wasif On BEST ANSWER

I will use this in powershell:

dir "filepath"-filt *.png -rec|ren -newn {($_.basename -replace "\d{4}$","")+$_.extension}
  • dir (alias to Get-ChildItem) will recursively list .png files. (-rec -> -recurse,-filt -> -filter)
  • Then pipe to ren (alias to Rename-Item)
  • Then create a scriptblock inside -newn (goes to -newname)
  • Replace last 4 digits from base name (file name without extension) using regex (\d matches a digit, {4} matches exactly 4 instances, $ matches at end of string) and join the extension, $_ goes to the pipeline item.
0
Doug Maurer On

Similar to Wasif's answer, but makes sure the name is at least 5 digits long so you don't end up with any files named .png.

Get-ChildItem -path $folder -Filter *.png |
    Where-Object {$_.Name -match '.{5}.png'} |
        Rename-Item -NewName {$_.BaseName -replace '.{4}$',".png"}
0
reapersremorse On

i found an even simpler way, open your folder in visual studio code, install a plugin called Batch Rename by jannisx11 v0.0.3

once installed, select all folders or files you wish to change the name of, then highlight the bit you want to replace, then you can either find and replace or use ctrl+d in order to select next untill all files that need changing are selected. now you can change the name just by typing. once you are ready you can save and it will auto rename all files for you. 1genericitem -> 1generic this is not an answer to the original question when considering i asked for a cmd or power-shell example, but i was under the impression that was the only 2 ways of doing this until i did more research, i should have done more thorough research in the beginning.