Is there a way to bulk rename files with overlapping names in Windows?

  • Thread starter Thread starter DaveC426913
  • Start date Start date
  • Tags Tags
    File pc
AI Thread Summary
To consolidate thousands of photos stored in multiple subfolders on a PC, users face challenges due to overlapping filenames. Solutions discussed include using a Perl script or DOS commands to bulk rename files before moving them to a single folder. A provided bash script example demonstrates how to prepend a unique number to each file based on its original folder, ensuring no filename conflicts. For Windows users, Cygwin can be utilized to run bash scripts, but alternatives like Windows Script or Python can also be effective. A batch code example is shared for copying files from a CD to a designated folder while renaming them sequentially. This approach allows for efficient organization of images without filename duplication.
DaveC426913
Gold Member
Messages
23,831
Reaction score
7,824
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?
 
Computer science news on Phys.org
DaveC426913 said:
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?

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?
 
If you're using Windows, you could probably use a DOS rename command from the command prompt...
 
This is a very crude bash script to copy all your jpgs into one folder.

pmv.sh:

Code:
#!/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
 
dduardo said:
If your on windows you'll need cygwin to run this script.
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:
James R said:
If you're using Windows, you could probably use a DOS rename command from the command prompt...
Yeah, you got any ideas about how that'd be done?
 
DaveC426913 said:
Holy jumpin'! How big is this cygwin? I don't want to buy a new hard drive to use it once!

I'm pretty sure there is a lot 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.
 
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.
 
robphy said:
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.

Thanks, Rob. http://www.fpschultze.de/modules/news/ 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:
===== 
@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 
=====
 
Back
Top