0

I am trying to add a prefix to all filenames in a folder.

After a couple of days of searching the internet I actually came up with something that almost work perfectly:

echo        Please specify fileprefix
echo            Your Choise??
set /p pre= ^
for %%i in (*.txt) do ren %%i "%pre%"%%i

The problem is that, if the prefix starts with a character that is alphabetically after the first character of the filename, then the first file is renamed twice.

Here is the output when trying to rename the three files 1.txt, 2.txt and 3.txt in the test folder:

Your Choise??
pre

D:\rntest>ren 1.txt "pre"1.txt

D:\rntest>ren 2.txt "pre"2.txt

D:\rntest>ren 3.txt "pre"3.txt

D:\rntest>ren pre1.txt "pre"pre1.txt

Leaving me with the three files pre2.txt, pre3.txt and prepre1.txt

The problem seems to be that the first files are sorted automatically by alphabetic order during the process. The first file is therefore moved to the bottom and is therefore being processed again. However, I have no idea whether this is what actually happens.

I have tried fiddeling around with a debug to rename the file again after the initial renaming.

Here is the code

for %%i in ("%pre%""%pre%"*) do ren %%i %%i:~3%

This seems to identify the correct (the double prefix) filename but it does not do anything with it (filename does not exist or there is already an identical filename). It was supposed to remove the first three characters of the filename to give me the correct result.

I just thought that there must be some better/easier way to do this without having to errorcheck and correct.

2
  • At least you didn't tag this question ms-dos :) Welcome to Stack Exchange and SuperUser.
    – user
    May 16, 2014 at 22:08
  • An easy solution would be to pre-process the folder creating an array of files to be renamed. After that, you can then interate over the array renaming the files. Otherwise you're working from a changing set. I cant test this suggestion (on mobile) but it looks like you should be able to easily adopt it.
    – nerdwaller
    May 16, 2014 at 23:19

2 Answers 2

1

EDIT: similar that nerdwaller was commenting about

You can use the dir command in a for /f loop to help give you expected behavior like so:

@echo off
echo        Please specify fileprefix
echo            Your Choise??
set /p pre=
for /f "tokens=1* delims=" %%i in ('dir /b /o:-n /a:-d *.txt') do ren "%%i" "%pre%%%i"

dir command switches explained:

/b    - Uses bare format (no heading information or summary).
/o:-n - Sort backwards (helps when you have file named similar names like 1.txt and 11.txt)
/a:-d - Don't give any directories (give files only)
1
  • Thanks alot, this worked like a charm :) and I almost understand all of the code :D
    – Hannibal83
    May 17, 2014 at 8:46
1

I found a very powerful free utility called Bulk Rename Utility for modifying Windows file-names.

It works very well and I donated some money through PayPal;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .