Windows rename multiple files

Solution 1:

Why not use some more direct and simple for loop command?

  • You don't need for any if in your loop:
❌ ren *.png ???-p.*  
✅ ren *.png p-*.png  
for %i in ("c:\your\folder\*.png")do ren "%~i" "p-%~nxi"
  • Some further reading:

    [√] ren /? | rename /?

Solution 2:

Append a string (or characters) to the beginning of file names

You can use a for loop to iterate each file in a directory and use variable substitutions to get the specific name portion of each file. You can use those and add in the "p-" string and append it as a prefix with the ren command for each file getting the expected output result you desire.

Essentially this. . .

  • Iterates all *.* files in a specific directory (not recursively)
  • Uses variable substitutions for getting the file name portions from each file
  • Appends the p- string to the beginning of each files and passes that per file as the second argument to the ren command for the new name

Command Line

for %a in ("C:\path\*.*") do if [%~xa]==[.png] ren "%~a" "p-%~Na%~Xa"

Batch Script

SET "Src=C:\path"
SET "Str=p-"
for %%a in ("%Src%\*.*") do if [%%~xa]==[.png] ren "%%~a" "%Str%%%~Na%%~Xa"

Further Resources

FOR

  • Variable Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    
  • Ren

  • Variable Substitutions
  • If