# Bulk file renaming - PC

1. Sep 25, 2005

### DaveC426913

I've got a couple of thousand pictures in a zillion subfolders (by year/month/day) that were produced by some Mac iPhoto camera software or something. (I am on PC.) It is really a pain to try to work with these files when I can only see a few at a time. I'd like to put them all into one folder. Problem is, many of them have overlapping filenames (eg. IMG0001.JPG).

Is there any type of utility, or even some sort of macro that would allow me to bulk alter the names on these files so I could put them all in the same folder together?

2. Sep 25, 2005

### ComputerGeek

if you were on a mac, I would say do it in automator, but since you are on a PC, why not just write up a perl script to do it?

3. Sep 25, 2005

### James R

If you're using Windows, you could probably use a DOS rename command from the command prompt...

4. Sep 26, 2005

### dduardo

Staff Emeritus
This is a very crude bash script to copy all your jpgs into one folder.

pmv.sh:

Code (Text):

#!/bin/bash

COUNT=0
EXT="jpg"
PBDIR=""

mkdir $2 find$1 -type f -iname \*.$EXT -print | while read file do PDIR=dirname$file

if [ "$PBDIR" != "$PDIR" ]
then
PBDIR=$PDIR COUNT=expr$COUNT + 1
fi

BNAME=basename $file mv$file "${2}/${COUNT}_\${BNAME}"
done

If your on windows you'll need cygwin to run this script.

Create a file called pmv.sh and stick the code inside. Make sure it is executable:

chmod +x pmv.sh

Then run it:

./pmv.sh <base directory to search> <folder name you want to stick your files in>

This script will pre-append a unique number to the jpg based on the folder it came from and then stick the file with the rest of your pictures.

Example:

1_pic1.jpg
1_pic2.jpg
1_pic3.jpg
2_pic1.jpg
2_pic2.jpg
2_pic3.jpg

5. Sep 26, 2005

### DaveC426913

Thanks. This'll do the job.

Holy jumpin'! How big is this cygwin??? I don't want to buy a new hard drive to use it once!

Last edited: Sep 26, 2005
6. Sep 26, 2005

### DaveC426913

Yeah, you got any ideas about how that'd be done?

7. Sep 26, 2005

### dduardo

Staff Emeritus
I'm pretty sure there is alot of junk that you don't need to install. You pick which packages you really need, namely: shellutils, coreutils and bash.

It also isn't my fault you're not running linux.

8. Sep 26, 2005

### robphy

While cygwin is a great set of packages for Windows, I wonder if you could get by with an implementation of bash for Windows, like http://www.steve.org.uk/Software/bash/ .

One could also write a version of the script in perl or python and then download a perl package like http://aspn.activestate.com/ASPN/Downloads/ActivePerl/ or a python package like http://www.python.org/ .

One could also try a Windows Script version.
(http://www.fpschultze.de/modules/news/index.php?storytopic=1&start=20 may point to useful examples.) I have no experience with Windows scripting.

9. Sep 27, 2005

### DaveC426913

Thanks, Rob. Frank Schultze has graciously provided a solution that looks like it will do exactly what I want:

The batch code below copies all files from CD to C:\TARGET using the file name IMG[00001..99999].JPG.

Code (Text):
=====
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET I=0
SET CDLETTER=D:
SET TARGET=C:\IMAGES
MD %TARGET% >NUL 2>&1
:LOOP
ECHO Insert ^(first/next/last^) image CD and press any key. Or press STRG+C to cancel
PAUSE > NUL
FOR /R D:\ %%F IN (*.*) DO (
SET /A I += 1
IF !I! GTR 9999 (SET F=IMG!I!.JPG
) ELSE IF !I! GTR 999 (SET F=IMG0!I!.JPG
) ELSE IF !I! GTR 99 (SET F=IMG00!I!.JPG
) ELSE IF !I! GTR 9 (SET F=IMG000!I!.JPG
) ELSE SET F=IMG0000!I!.JPG
COPY /Y "%%F" C:\IMAGES\!F!
)
GOTO LOOP
ENDLOCAL
=====