Rename all files in a folder

I’d like to find a Windows-based program that would do the following:
There are sub-folders A, B, C, etc. in a given folder. In each of those folders there are a number of picture files with generic names like pic1.jpg, etc.

I’d like the program to go into sub-folder A and rename the files A_01.jpg, A_02.jpg, etc. Then continue into sub-folder B and rename all the files B_01.jpg, etc. until al folders have been renamed.

It’s important that the files all have two digits. That is I don’t want A_1, A_2, … A_9, A_10.

This could be done with a command line batch file, but the problem I get is it seems to name the files in order of creation date (maybe) rather than in alpha-numeric order based on their current names.

Any suggestions?

Do you want a free program, or are you willing to pay for it?
What version of Windows?
Would you be willing to use a free version if it required you to write your own script? You could download and install a Unix emulator, like cygwin, and then write a UNIX shell script. But then you would think that what you are trying to do is possible with Windows PowerShell.

I have seen people use this: Better File Rename but it’s not free. It seemed pretty easy to use.

That sounds like a programming task albeit a fairly minor one given your requirements. Can you back up a step and tell us why you need to do it that way and how often it needs to be done? It sounds fairly unusual. Cygwin basically lets you use Unix commands within Windows as noted and it can do whatever you want like this for free but it isn’t easy to use for someone that isn’t familiar with Unix style programming.

Thanks for the replies.

Arnold Winkelried: I’d prefer free of course, but $20 isn’t too much. I’m not sure the program you suggests does all subfolders at once. It looks like Id have to suggest the name for each of them.

Shagnasty: I don’t know UNIX. I have worked some with DOS batch commands. I have from my parents a huge number of digital photos. They are all named pic01, pic02, etc. and are in separate folders which I’ve identified as various events (FL vacation 2001, Xmas 2003, etc.) by looking at them. Now I’d like to change all the picture names to FL_vacation_2001_01.jpg, etc. so the pictures can be identified uniquely without referring to the folder. I do have a batch renaming program (or I can even use IrfanView to do it), but there are hundreds of these folders and I’d prefer not to have to use one of those program on each folder separately and was looking to automate the process so it could be invoked once and applied to all folders using the folder’s name as a prefix.

If you’re using a “for” command in a Windows *.bat file, you need to double up the “%” in front of the variable you’re using in the loop. I don’t know if that’s your issue or not.

How are you with Unix/Linux command line? Perl? Python? You might be better off writing what you need in a “more flexible” scripting language. I usually use Cygwin, but found that the Git version for Windows includes a basic “BASH” shell which can do quite a bit and it is a lot faster to download and install (less than 5 minutes vs. up to 2 hours). I’d do that even if I don’t want Git.

I’ll have to play around a bit with Windows to really figure out the command. However, if you’re okay with Bash (Unix shell) or Perl, I can write you up something pretty quick.

Total Commander has a multi-rename tool that would probably work for you. It is shareware but it never expires and is fully functional even if you don’t buy it.

The only downside is the user interface is not the greatest. However, it would still be easier than writing batch files.

I don’t know what you mean by a huge amount of digital photos but as a professional systems analyst, I don’t automatically discount the various brute force approaches to this type of thing either. You could open up each folder one by one and set up the rename to append what you want to the file name as a identifying first step without worrying too much about getting the perfect file name in total as long as it has the main identifier you want. When that is done, you just copy the files into one folder and do searches and replaces from there to clean up the rest of the names way the most efficient way you can and correct the rest manually. Done correctly, it should only take a few minutes which is much faster than any programming solution.

I have never used it myself but this sound like a good utility to try and it is free.

I just tried the tool above. The ‘Advanced’ tab will let you append together whatever you want about the file as a new file name with the ‘Insert Keyword’ command at the bottom left. I choose <folder> and <filename> but you can add dates and lots of other things as well. Be sure to ‘Preview’ the results before you ‘Apply’ it and that does most of what you described in about 30 seconds.

The closest I could come in Batch is something like this:



for /R %i in (*.jpg) do move %f~i %p~i


But, that won’t work because %p~i is the full path name and not the name of the directory. :frowning:

Your best solution is to download and install Git. You aren’t interested in Git, but the Git BASH program that has a lot of basic Unix tools. The whole thing takes 3 minutes to download and install. You can do Cygwin, but that’s more than you need and the last time i installed that, it took 2 hours.

[ul]

[li] First: BACKUP THE DIRECTORY THAT CONTAINS ALL OF YOUR Pictures. You want to make sure you don’t do anything irreversible.[/li][li] Download Git. You can get it from Google Code Archive - Long-term storage for Google Code Project Hosting.. The first link is the one you want.[/li][li] After it downloads, Install it. Just take the defaults.[/li][li] There will be an Icon on your desktop for Git BASH. Open that up.[/li][li] Now, you’ll have to change to the directory that contains the pictures. You’re heart will feel some palpitations since you’ll be doing this from the command line, but this is fairly harmless. The command to change directories is cd – just two letters. The tricky part is that directory names in this shell are separated by forward slashes ( / ) – the one by the question mark, and not the backward slashes ( \ ) that are found above the Return key. Also, Drive letters are surrounded by slashes. The C: drive is /c/ and the D: drive is /d/. Also remember to put quotes around the name if there are spaces in the name.[/li]
The easiest way is to move one directory at a time. For example, if your pictures are under C:\Documents and Settings\Old Guy\Desktop\My Pictures, you’ll be executing this series of commands:



$ cd /c/
$ cd "Documents and Settings"
$ cd "Old Guy"
$ cd "Desktop"
$ cd "My Pictures"


You can use the “pwd” command to see your current directory and use the “ls -F” command to see all the directories and files in that directory.

Once you’re in the directory you want to be in, you’ll be typing in the following command in at the command line. The “>” are a continuation prompt, so you don’t type that in.



$ find . -type f | while read file
> do
> basename=$(basename "$file")
> dirname=$(dirname "$file")
> current=$(basename "$dirname")
> echo "moving file '$file' to '$dirname/${current}_${basename}'"
> mv "$file" "$dirname/${current}_${basename}"
> done 


[/ul]
When you type in “done” the progrm will go through and rename all files in that directory from foo.jpg A_foo.jpg where “A” is the directory name.

I’ve tried this command myself, but you’ll have to realize I did it on my Mac in the BASH shell, so I can’t guarantee exactly how it’ll work.

I recommend to have a full bottle of Scotch at the ready. You can take a congratulatory swig if it works, or drown your sorrows in case this goes bad.

In fact, let’s wait to see if people can pick this apart for possible syntax errors.

Bulk Rename Utility can do it easily.

I’ve always used Rename Master, it’s free.

Lupas Rename it’s free and easy to use. I’ve used it since 2005 and love it.

pc world has ver 4
http://www.pcworld.com/downloads/file/fid,52120-page,1-c,filemanagement/description.html

the author released ver 5 on his web site
http://rename.lupasfreeware.org/

I’ve used both versions and like them both.

yoyodyne Bulk rename works perfectly for me. Thanks

And thanks to everyone else who helped.

aceplace57: I’ve also used Lupas and like it a lot. But I couldn’t see how to incorporate the folder name into the filename except manually typing it in for each folder.

I’ve done this an even more brute force way:

  1. do a bare directory list of the folders’ files, redirected to a file: DIR /B >Myfile.bat
  2. Using an appropriate text editor, change each line in that file to a REName command.
  3. From a DOS box, run that batch file.

This is especially helpful if there are some files in the folder you do NOT want to rename, or if some of the files are not named consistent with the pattern.

I know the OP has already found an acceptable solution, but I enjoyed the challenge. Here’s a batch file that fits your description.


@echo off
setlocal EnableDelayedExpansion
  subst B: .
    for /R B:\ %%I in (.) do (
      set /a number=0
      for %%J in ("%%I\*.jpg") do call :LOOP "%%~fJ" !number! 
    )
    for /R %%I in (.) do ren "%%I\*.tmp" "*.jpg" 2>NUL
  subst /d B:
endlocal
GOTO:EOF   

:LOOP
  set /a num=%2+1
  if /i %num% LSS 9 set number=0%num%
  if "%number%" == "" set number=%num%
  set dir=%~p1
  if "%dir%" == "\" ( set file=%number%&goto OUTPUT )
  set dir=%dir:~1%
  set dir=%dir:\=_%
  set file=%dir%%number%
:OUTPUT
  ren %1 "%file%.tmp" 2>NUL

The hardest part was dealing with Microsoft’s [del]bugs[/del]unusual implementation of how variables expand. Using delayed expansion and trying to manipulate the strings did not want to work.

I bow down to your superior geekitude. It is an amazing piece of batch code.

I haven’t tried getting the folder name into the filename myself. I primarily use Lupas Rename for mp3 files I’m renaming for my personal player or jpegs.

He added a few nice features to ver 5. I like being able to use columns to change letters in the middle of a filename

i.e. … this_is_a_file02_example.jpg. this_is_a_file05_example.jpg

changes to… this_file_example01.jpg, this_file_example02.jpg

a string substitution won’t easily work because the string isn’t the same in every file. Instead, you can start the change in column 15 replacing 2 letters with nothing. Autonumbering takes care of the rest.

Shame that he stopped updating the app in 2005. No telling what other features he could have added.

I’ve been using “The Rename” for many years and it’s free. You can add nuber to the end of th existing file name, or create completely new file name with just a few clicks.

To one **OldGuy **from another-I appreciate and understand your need to i.d. your pix better. After recently buying a slide scanner, I am in the process of scanning 2000+ slides from my 2 years in and around the Mediterranean and also downloading and i.d.ing pix in 3 more recently fulled SD cards from my digital cams. Each batch of scanned/uploaded slides gets tagged by the software with its upload date so I’m going thru each one and sorting/labeling them into Eur-Turkey-Istanbul-Topkapi, Asia-the Holy Land-Dead Sea & Me, Africa-Morocco-Tangier-Belly dancer, etc. I’m using Picasa3 but haven’t gotten into any fine points of that yet.

Pardon the hijack but my biggest concern so far is that most of the overseas slides were taken with an Instamatic camera with 26-mm square format vs. 35 wide so the tops and bottoms are slightly cropped and there are vertical black bars on each side. I’m wondering if I were to sshave/file 1/16th to 1/8" off the top & bottom of the hard plastic slideholding sleeve that runs thru the scanner, would it let me show the full height of the pic?