File and Folder Renaming

Okay, I am in the process of trying to write an applescript to rename a set of images contained in folders for our web site. The images have the same name as their containing folder with the addition of .jpg on the filename. Just to clarify, each image has it’s own folder, one image per folder.

We are switching from 10 digit ISBN numbers to 13 digit and while I have already written a script in filemaker to create a new field and convert the 10 digit number to 13 for our database, the renaming of the folders and files is quite daunting.

Here’s what needs to happen -

I need to copy the folders and their files to a new folder, rename the folder with the converted ISBN number, and rename the contained file with the converted ISBN number and the .jpg suffix.

Am I making this too complex? I have been wracking my brain for a while now trying to figure this out. It seems like a natural for a script to me, but wow.

Thanks in advance for any help,

Andrew

Hi

I might need some more info but it sounds like a job for applescript and filemaker

A suggested plan ( if all image folders are within the same folder) would be

Create in file maker a field that contains the old image number less the “.jpg” (OldnumNoext)
Create in file maker a field that contains the new image number less the “.jpg” (NewnumNoext)
You should also have fields that have the new and old image numbers
List the folder that contains the image folders
Get records from filemaker
create repeat loop
from each record get OldnumNoext, NewnumNoext, Oldimagenum and Newimagenum
using the variables create a path to the folder ( theListfolderpath & OldnumNoext)
rename folder with NewnumNoext
using the variables create a path to the image ( theListfolderpath & NewnumNoext & Oldimagenum)
rename image with Newimagenum
end repeat

This of course presumes you can write applescript!!!
THis is only a guide and I am sure could be simplified but the task is not as daunting as you night have first thought

What you want to do is certainly scriptable; the Finder can do what you have in mind. Before embarking on a method, however, would you give us a sample of names like this?

folderA1234567890 {containing ImageFileA1234567890} is to become

etc.

Thanks for the replies,

As you may have guessed I am a neophyte when it comes to Applescript, so talk to me like I am an idiot.

folder0-7787-0131-X {containing ImageFile0-7787-0131-X.jpg}

is to become

folder978-0-7787-0131-6 {containing ImageFile978-0-7787-0131-6.jpg}

Thanks for you help!

Andrew;

Can I assume from your sample that all that’s going to happen here is that the leading digit on a folder and file name (following any text portion (is there any?), will be changed to three digits, and that the trailing “X” (some number or letter, or actually always an “X”?) will be changed to a single digit? Are these changes the same for every folder/file name or are they supplied from a list?

You say: “I need to copy the folders and their files to a new folder, rename the folder with the converted ISBN number, and rename the contained file with the converted ISBN number and the .jpg suffix.” Can we assume all of the old folder/files are in an enclosing folder to begin with and that the target folder exists?

On my desktop I set up a folder with a jpg file in it like yours, and a new folder. This script moves the folder and file with a new name. This script will give you a framework of how it’s done, so ask questions if it’s not clear. I assumed that the names were pure number sequences.

--set samp to "0-7787-0131-X {containing 0-7787-0131-X.jpg}"
--set fin to "978-0-7787-0131-6 {containing 978-0-7787-0131-6.jpg}"

property leader : "978"
property trailer : "6"
set target_folder to choose folder -- the enclosing folder of the old folder/file pairs
set new_folder to choose folder -- the folder to which the new folder/enclosed file will go

tell application "Finder"
	set fldrs to every folder in target_folder
	repeat with F in fldrs
		set itsFile to every file in F as alias -- supposed to be only one, but need the "every"
		-- assuming that every file name ends with ".jpg"; otherwise more elaborate scheme required to extract the extension and add it back later.
		set itsName to text 1 thru -5 of name of (info for itsFile) as string -- clip off the .jpg
		set newName to my fixname(itsName) -- need it without the .jpg for the folder name.
		set NF to make new folder at new_folder with properties {name:newName} -- an error if one already exists
		set tF to duplicate itsFile to NF with replacing -- still has the old name
		set name of tF to newName & ".jpg" -- adds extension back and renames the file
	end repeat
end tell

to fixname(oldName) -- add new leader and trailer from above
	set astid to AppleScript's text item delimiters -- record what they were when we started
	set AppleScript's text item delimiters to "-" -- set them to dashes
	set N to text items 2 thru 3 of oldName -- grab the middle two chunks between the dashes
	set N to {leader} & N & {trailer} -- add the new beginning and ending in a list
	set fixedName to N as string -- stick them back together with dashes between (text item delimiters still dash)
	set AppleScript's text item delimiters to astid -- put the delimiters back to what they were
	return fixedName -- the new setup
end fixname

Yes the leading digits will always be 978- but the final digit is calculated and is not consistent.

I have both the 10 digit and 13 digit listed in a filemaker database and I was hoping to just look them up to change the name.

The group of folders are all in one enclosing folder as well.

I will give your script a try.

Thanks very much for your help!

Dude, that is slick! Now if you can help me get the final digit from filemaker or text, I will owe you big time!