3

I want to put 1 character at start of each jpg filename. I have made this batch file:

for %%A in (*.jpg) do ren "%%~A" s"%%~nA%%~xA"

This works almost perfectly, but the first file is processed twice:

ssNL201501
sNL201502
sNL201503

What do I do wrong?

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
janklaas
  • 33
  • 1
  • 4

1 Answers1

4

This works almost perfectly, but the first file is processed twice

for %%A in (*.jpg) do ren "%%~A" s"%%~nA%%~xA"

You need to use:

for /f %%A in ('dir /b *.jpg') do ren "%%~A" s"%%~nA%%~xA"

As dbenham explains in his answer to add “text” to end of multiple filenames:

Note that it is critical that you use FOR /F and not the simple FOR. The FOR /F gathers the entire result of the DIR command before it begins iterating, whereas the simple FOR begins iterating after the internal buffer is full, which adds a risk of renaming the same file multiple times.


Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394