I have a folder with images :


And I want to rename this files adding a p- as follows:


How can I achieve this? I tried

ren *.png ???-p.* 

but this is giving the opposite results.

  • You can use either a batch script or a tool like den4b.com/products/renamer . Look also at the Related links on the right of this page. – FedonKadifeli Jul 14 '19 at 12:10
  • some more info on ren can be found here : superuser.com/questions/475874/… – Pathfinder Jul 14 '19 at 13:09
  • @NexussimLements .... Did you look over the cmd batch file rename method as described and such below which I wrote up for you a while back? – Pimp Juice IT Aug 23 '19 at 1:02
  • @Pimp Juice IT Yes , it worked like a charm. Thanks (I don't have enough reputation to upvote your answer.) – Nexussim Lements Aug 23 '19 at 10:24

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


  • 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.