2

I have a folder with images :

1.png
2.png
3.png
...
11000.png.

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

p-1.png
p-2.png
...
p-11000.png

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 https://www.den4b.com/products/renamer . Look also at the **Related** links on the right of this page. – FedKad Jul 14 '19 at 12:10
  • some more info on ren can be found here : https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards – PathToLife 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? – Vomit IT - Chunky Mess Style Aug 23 '19 at 01: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

3 Answers3

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
Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
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"
Io-oI
  • 7,588
  • 3
  • 12
  • 41
0

Total Commander has a Ctrl+M nice filename changing tool.

It can replace strings, use RegEx, counter, range of characters, upper/lower-case and few other.

pbies
  • 2,757
  • 3
  • 21
  • 23