HowTo – When You Need To Rename A Lot Of Files Or Folders

Filed under: Site Management

There are times when you are managing data that you will need to rename a large number of files.  There are a few ways to do this without losing your mind.

A few applications of this process may be when you are moving hosts or when you are given a number of files from a client that need to be imported into their new site.

You might also use this process to duplicate sites or code base or just remove the clutter from your life.

For this HowTo we will be covering ways to do it on a Windows based computer but many of the ideas can be applied to other systems.

The first thing you should do is grab the files you want to manage and COPY them in the root directory of your drive. If the files are already in folders copy the whole structure. Moving them to the root will eliminate some headaches.

We should also say when doing something like this it is always best to work on a copy that you can verify and not work on production files. This may be imposible in your situation so do the best you can.

Get a Listing of your Files

Now you need to get a listing of your files into a text file.

The Dos command DIR has a lot of switches that will allow you to format the way you see your files.

Probably the best way to get a listing of all your files with the directory in front of it is to do a:

dir /B /S

This will give you a listing of each file and its location on the drive. It will look something like this:

C:topdirectorysubdirectoryfilename.ext

You may also want to try a plain DIR /S but this will not place the Directory name on the front of the file name It will however sort the files within each directory into groups. It will also give you garbage info like date and sizes.

Putting It In A File

Once you decide which structure is best for you to edit you will need to output this directory info to a text file:

dir /B /S >myfiles.txt

This will create a file in your directory named myfiles.txt with the directory output.

Edit The Directory Listing

Now that you have a full list of files you will need to edit it in a text editor to prepare the list for renaming.

Open the file and strip out any useless info like file sizes if needed.

Once you are done copy the file and name it oldfilenames.txt

Using your Spread Sheet
Open a new spread sheet

Count the number of lines in your directory listing and highlight that many lines in the second column of your spread sheet and past in the results. This is your starting name.

Now place the word copy in the first column of your spread sheet and duplicated it for as many rows as you have in column 2.

Setting your Final Name
The way you do this is up to you but it will require a number of find and replace actions to be performed on your original oldfilenames.txt file.

Start by copying the file to newfilenames.txt

perform any find and replace actions you need to do such as changing directory names or file names.

Double check the results then paste the results into your Spread Sheet in column 4. You need to keep column 3 blank.

Making your batch file

Once you are happy that the original name in column 2 has been changed to your final result in column 4 open a new text file in the root folder where you stored your files. Name the file mybat.bat now copy rows 1, 2, 3, 4 from your spread sheet into your bat file.

Once you are happy everything looks right save the batch file.

Now execute the batch file by double clicking it or typing its name into a command prompt c:mybat.bat

The batch file will execute and copy your files to their new locations with new file names.

Final Note

There are many methods of creating a batch file to move copy rename your files to a new structure and file name association.

If you find this method too complex and it can be time consuming you may want to look at a number of freeware applications that can help you do the work.

Irfanview is a great image viewing program but it also has the ability to rename image files and create directory structures.

If you are working with other file formats you should check on Simtel.net , Source Forge or another archive site.

This is a relatively difficult task if you do not plan and understand exactly where your file are and where you want them to be.

If you find that this task is something you need to do routinely then you may want to look into building a perl program,  .bat file or other executable that can do the work as needed.

The reason we did not simply use a simple copy or other dos command to perform this task is because you need to have a way to control each and every file and directory.

If you choose to simply move all of your files or rename them with a prefix 0001 xxx whatever then a command line tool may be sufficient.