Apple Event: May 7th at 7 am PT

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

May 9, 2015 4:24 PM in response to Twitcheye

Great! As a bonus, here's a much better version of the script:


set theUserName to "JohnSmith"

set theNewFolderName to "Folder X"

set L to 17 -- the initial length of every file name (like “IMG_20150424_0001”) without its extension


tell application "Finder"

set theFolders to selection as alias list

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

set {x01, y01, x02, y02} to get bounds of desktop'swindow

end tell


tell application "TextEdit" -- open a new document where to provide progress feedback

launch

activate

set theDocument to makenewdocumentat front

delay 0.25

set {x1, y1, x2, y2} to bounds of window 1

set bounds of window 1 to {(x02 - x2 + x1) / 2, y02 / 2, (x02 + x2 - x1) / 2, y02 / 2 + 200}

end tell


tell application "Finder"

with timeout of 3600 seconds-- one hour

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

my displayMessage("Deleting files of folder “" & theNewFolderName & "”.")

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

end if

set N to 0

set P to L + 1

repeat with thisFolder in theFolders

set thisFolderName to name of thisFolder

if thisFolderName is not theNewFolderName then

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

my displayMessage("Duplicating files of folder “" & thisFolderName & "”.")

set theNewFiles to duplicatetheFilestofoldertheNewFolderName of theContainer

my displayMessage("Renaming files of folder “" & thisFolderName & "”.")

repeat with thisFile in theNewFiles

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

if (length of thisFileName) > L then -- name with extension

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

& textP through -1 of thisFileName

else -- name without extension

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

end if

set name of thisFile to thisNewFileName

end repeat

end if

end repeat

end timeout

end tell

tell application "TextEdit" to closetheDocument without saving

on displayMessage(theMessage)

global theDocument

tell application "TextEdit"

activate

set theDocument'stext to return & theMessage

set size of theDocument'stext to 24

end tell

end displayMessage

May 10, 2015 12:57 PM in response to Pierre L.

Pierre,


Before I start fiddling with it, could you explain in few words, what is this new version doing different/better than the previous one? I mean, I can see that it asks the "TextEdit" app to write a report on a progress, but how is this useful to me?


I understand that it might be of some use to the developer, but from my standpoint as a user, I would rather have some sort of an UI than a report… or both. Or, have it rename the files properly regardless of the length of original file names (so that I can use the script for files generated by various hardware/software combinations).


But I am grateful nonetheless for all your patience and time. This script in the previous version already saved me a lot of time.


Thanks again!

May 10, 2015 1:10 PM in response to Twitcheye

Well… maybe “a much better version” was somewhat exaggerated… The new version adds just that: providing continuous feedback on progress. Which might be useful when you have many folders containing many big files, by telling you what the script is currently doing, for example "renaming files of folder 4” or “duplicating files of folder 5”.

May 10, 2015 1:33 PM in response to Twitcheye

Twitcheye wrote:


I would rather have some sort of an UI than a report… or both. Or, have it rename the files properly regardless of the length of original file names (so that I can use the script for files generated by various hardware/software combinations).


The new version also allows you to choose the length of the original file names at the very beginning of the script, by changing the value of L.

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.