Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Renaming files inside several folders

Hi, guys,


I am not a scripter and I got not quite enough time to learn scripting, especially so, that my needs for using scripts are very sporadic. I wonder if any of you would help me to get up and running with a script (AppleScript or Automator) to accomplish something like this:


I got several folders full of scanned images. They are named in a way specific to the scanner workflow. I would like to change that naming scheme and combine all files into a single folder.


So, for instance, "folder 1" contains:


  • IMG_20150424_0001
  • IMG_20150424_0002
  • IMG_20150424_0003
  • etc…


"Folder 2" contains:


  • IMG_20150426_0001
  • IMG_20150426_0002
  • IMG_20150426_0003
  • etc…


"Folder 3" contains:


  • IMG_20150429_0001
  • IMG_20150429_0002
  • IMG_20150429_0003
  • etc…


Now, I want to put images from all folders into one folder, but, before I do that, I want to rename contents of each folder so that they are in a continuous sequence from 0001 to 0009 (as per example above). As a result, I want to get something like this:


Single "Folder x" contains:


  • IMG_0001_JohnSmith
  • IMG_0002_JohnSmith
  • IMG_0003_JohnSmith
  • IMG_0004_JohnSmith
  • IMG_0005_JohnSmith
  • IMG_0006_JohnSmith
  • IMG_0007_JohnSmith
  • IMG_0008_JohnSmith
  • IMG_0009_JohnSmith
  • etc…


If any additional information is needed, please, ask.

MacBook Pro with Retina display, OS X Mavericks (10.9.5)

Posted on Apr 29, 2015 11:55 AM

Reply
20 replies

Apr 29, 2015 3:32 PM in response to Twitcheye

You might try the following script, which can be run from the Script Editor window. Just select your folders and run the script.


set theUserName to "JohnSmith"

tell application "Finder"

set theFolders to selection as alias list

if theFolders is {} then tell me to return beep 2

set theFolders to sorttheFoldersbyname

if not (existsfolder "Folder X" of desktop) then

makenewfolderatdesktopwith properties {name:"Folder X"}

else

deleteitems of folder "Folder X" of desktop-- start with an empty folder

end if

set N to 0

repeat with thisFolder in theFolders

set theFiles to (get files of thisFolder as alias list)

repeat with thisFile in theFiles

set thisFileName to name of thisFile

set N to N + 1

if N < 10 then

set N to "000" & N

else if N < 100 then

set N to "00" & N

else if N < 1000 then

set N to "0" & N

end if

set thisFileName to "IMG_" & N & "_" & theUserName & ¬

text 18 through -1 of thisFileName

set name of thisFile to thisFileName-- rename the current file

end repeat

movetheFilestofolder "Folder X" of desktop

end repeat

end tell

Apr 29, 2015 4:36 PM in response to Pierre L.

@Pierre L.


Thanks a lot, Pierre. It works like magic. If you could make one modification to this original script, I would be even more grateful.


Right now, the script deletes all originally named files from selected folders when it renames them and puts them in the new single folder. How about letting them remain in the original location and in the unchanged state – just to have a backup of original files?

Apr 30, 2015 6:33 AM in response to Twitcheye

The following should do the trick:


set theUserName to "JohnSmith"


tell application "Finder"

set theFolders to selection as alias list

if theFolders is {} then tell me to return beep 2

set theFolders to sorttheFoldersbyname

if not (existsfolder "Folder X" of desktop) then

makenewfolderatdesktopwith properties {name:"Folder X"}

else

deleteitems of folder "Folder X" of desktop-- start with an empty folder

end if

set N to 0

repeat with thisFolder in theFolders

set theFiles to (get files of thisFolder as alias list)

duplicatetheFilestofolder "Folder X" of desktop

repeat with thisFile in result

set thisFileName to name of thisFile

set N to N + 1

if N < 10 then

set N to "000" & N

else if N < 100 then

set N to "00" & N

else if N < 1000 then

set N to "0" & N

end if

set thisFileName to "IMG_" & N & "_" & theUserName & ¬

text 18 through -1 of thisFileName

set name of thisFile to thisFileName

end repeat

end repeat

end tell

May 3, 2015 4:18 PM in response to Pierre L.

@Pierre L.


Sorry to bother you again. I got one more question about the script you've created for me.


Suppose, I want to create that new folder with continuously sequenced images in a different location than desktop... Let's say, I want it to be in the same directory as the original folders with unsorted images. How would you accomplish that? Below is the particular directory I would like to use:


/Volumes/ARCHIVE 2/MASTERING/PHOTO/CANON Scanned Images/

May 4, 2015 7:45 AM in response to Twitcheye

You don't bother me at all. Here's an improved version of the script, where you can easily change both the user's name and the destination folder's name.


set theUserName to "JohnSmith"

set theNewFolderName to "Folder X"

tell application "Finder"

set theFolders to selection as alias list

if theFolders is {} then tell me to return beep 2

set theFolders to sorttheFoldersbyname

set theContainer to container of item 1 of theFolders as alias

if not (existsfoldertheNewFolderName of theContainer) then

makenewfolderattheContainerwith properties {name:theNewFolderName}

else

deleteitems of foldertheNewFolderName of theContainer-- start with an empty folder

end if

set N to 0

repeat with thisFolder in theFolders

if name of thisFolder is not theNewFolderName then

set theFiles to (get files of thisFolder as alias list)

duplicatetheFilestofoldertheNewFolderName of theContainer

repeat with thisFile in result

set thisFileName to name of thisFile

set N to N + 1

if N < 10 then

set N to "000" & N

else if N < 100 then

set N to "00" & N

else if N < 1000 then

set N to "0" & N

end if

set thisFileName to "IMG_" & N & "_" & theUserName & text 18 through -1 of thisFileName

set name of thisFile to thisFileName

end repeat

end if

end repeat

end tell

May 8, 2015 2:12 PM in response to Pierre L.

Hi, Pierre,


It seems that there is always something to be improved in a script...


I ran into an unexpected obstacle. I had over 360 files in one of the original folders and over 450 in the other one. When I tried to rename and move the contents of these folders to a new single folder, I got an error message saying that ''Finder got an error: AppleEvent timed out.' Number -1712 . It seemed that the error was occurring at line: "duplicatetheFilestofolder "FOLDER X" of desktop", because that line was highlighted when the script got an error.


Is there an easy way to avoid that?


Or, perhaps, the script could be modified in such a way, that it would just rename files in sequence in a single folder, then, I would change the starting number (in the script) by hand - for every consecutive folder, and then I would move renamed contents of each original folder to a new single folder - in sequence?

May 8, 2015 4:09 PM in response to Twitcheye

I have tested the script again, this time with 400 pdf scanned files in the first selected folder and 500 in the second. The script worked flawlessly. However, when duplicating such a great number of files, one has to wait for a rather long time until the script has finished renaming each batch of files.


On the other hand, I've found out that the script, in its current state, will stop running if some file has no name extension at all. Might it be the case with one of your scanned files. I surely could improve the script to cope with such an issue.

May 9, 2015 9:05 AM in response to Twitcheye

It seems that there is always something to be improved in a script...


You're perfectly right.


To begin with, you should replace

set thisFileName to "IMG_" & N & "_" & theUserName & text 18 through -1 of thisFileName

with

if (countthisFileName) > 17 then -- name with extension

set thisFileName to "IMG_" & N & "_" & theUserName & text 18 through -1 of thisFileName

else -- name without extension

set thisFileName to "IMG_" & N & "_" & theUserName

end if

in the script.

May 9, 2015 11:40 AM in response to Pierre L.

Hi, Pierre,


I changed the script according to your instructions. I started with two original folders containing a total of 842 image files weighing in at a total of 14.2 GB. Every file in those two folders had the extension ".tif".


However, the result of running this modified script is the same as before the change – script runs until the error message shows up at the line: "duplicatetheFilestofoldertheNewFolderName of theContainer".


Same message as before: "error 'Finder got an error: AppleEvent timed out.' number -1712".


At that point, Finder continues copying files to the new folder, but it stops when all files of only the first original folder are renamed and copied. The second original folder remains untouched.


What do you make of it?

Renaming files inside several folders

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.