0

I have 100 .png files in a folder say test.png, example.png, etc.

I want to prepend them with New_ so that it will be renamed as New_test.png, New_example.png etc.

How can I do it in shell.

3

You didn't specify an operating system. This answer assumes Windows.

My first thought was a simple command such as

for %f in (*.png) do rename "%f" "New_%f"

But a file named a.png was first renamed to New_a.png and moved down the alphabetical order :-( The loop renamed this file a second time to New_New_a.png.

Therefore, this must be done in two steps:

dir /b *.png > png.txt
for /F %l in (png.txt) do rename "%l" "New_%l"
del png.txt

The first line creates a text file containing the names of all png files in the current directory. The second line parses each line of the file and renames the png's.

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.