How to rename hundreds of files in less than 30 seconds

Posted 2013-09-20 10:48 PM GMT

I think a solid knowledge of computers is useful for everyone, and not just professional software developers (I built a Chrome extension to track my hours while working as an auditor). In this post I will discuss how accountants (and others) can use the command line (or Windows command prompt) to save several hours of work.

One of the first tasks I was given as an auditor at Ernst & Young was to copy all of the files from the previous year's audit into the current year's audit folder, then rename them with the correct year. So, accounts_payable_2010.xlsx would become accounts_payable_2011.xlsx, and inventory_walkthrough_2010.docx would become inventory_walkthrough_2011.docx. I thought the copying part would be easy, but then they told me not to copy any of the PDF's. So, I ended up copying everything, then deleting the copied PDF's.

The renaming part was the hard part. There were literally hundreds of files to be renamed. So, I opened up Windows' file viewer (called Windows Explorer) and started the painfully mundane task. Click on the filename, wait for about half a second, then click again (the second click lets you rename the file), then hit the right arrow, hit backspace, and then type "1", and repeat. About 40% of the time I didn't wait long enough before the second click, and Excel or Word would (slowly) load the file I was trying to rename. I would wait for the file to load, close it, then finish renaming the file.

After 15 minutes, I downloaded and installed Perl, spent 20 minutes writing a script to rename the files, ran my script, and I was done. I was pretty proud of myself, and my manager was shocked that I was already done.

Perl was the first programming language I learned, and I think it's pretty great. However, I'm trying to show you that you can rename hundreds (or thousands) of files in less than 30 seconds, and it would take you a lot longer than that to write a Perl script to rename all the files (especially if you don't already know Perl). So, I'm going to show you how to do it using the command prompt (or command line if you're using Linux or Mac).

1. Open the command prompt

Windows: Use the keyboard shortcut Windows key + r to open the run menu, then type cmd in the prompt, and hit enter.

Windows Run Dialog

Mac: Use the keyboard shortcut + spacebar to open Spotlight, and type Terminal

Mac Spotlight with Terminal as search term

Linux: Use the keyboard shortcut ctrl + alt + t to open the Terminal.

2. Navigate to the correct folder

When you open the Command Prompt or termnial, you will be in your "home" directory. Use the cd command to move to the correct folder.

Windows: Let's say the 2012 audit files are in C:\Users\sfoote\audit\2012, and I am currently in C:\Users\sfoote. I can use cd audit\2012 to move to the correct folder. Use the dir command if you're not sure what is in the current folder.

Windows command prompt, using cd to navigate

Linux/Mac: You use almost the same command, but use forward slashes instead of backslashes: cd audit/2012. Use the ls command to see what is in the current folder.

Linux terminal, using cd to navigate

3. Copy the correct files to 2013 folder

Windows: I think there is a way to copy only the correct files using Windows, but I couldn't get it to work, so we are going to copy all the files, then delete the ones we don't need (this part may be just as easy to do using Windows Explorer). If you're in C:\Users\sfoote\audit\2012, and you need to copy the files into C:\Users\sfoote\audit\2013, use copy * ..\2013\* to copy the files. Then, if we want to delete all the PDF files, first move to the 2013 directory (cd ..\2013), then use del *.pdf.

Windows command prompt, copy and del

Linux/Mac: On Linux and Mac you have the option of only including certain file types as you copy. Let's say we only want to copy .xlsx and .docx files; we can use cp *.xlsx *.docx ../2013:

Linux terminal, using cp to copy certain files

4. Rename the files

Windows: Use the ren or rename command (they both do the same thing) to rename a bunch of files at the same time: rename *_2012* *_2013*

Windows command prompt, rename files

Linux: Use the rename command: rename 2012 2013 *_2012*.

Mac: Mac does not support rename, so we have to do a little bit of scripting.

mac rename

I admit, it will probably take more than 30 seconds your first time, but it's still way faster than the alternative.