Sort files within folders and then make sequential with existing name

I have a huge automator workflow and although it works as planned it is very clunky.
I am hoping I can get rid of some of the steps with applescript but have hit a block with the above subject.
I’ve stumbled upon rsync, which is saving me about 20 automator steps but I can’t get my head around this bit.

This is the file structure that I currently have

and this is the files structure I’d like to end with

(I think I can work out how to remove the 3 digits at the start, and also remove the _X)

I need to sort the files within the folder by date creation and then make sequential. (oldest=0)
Now this only needs to really occur on the Tiff folders, but every tiff folder needs to be sorted individually and the counter reset to 0 for each folder. But if its easier to just script all folders at once then thats fine as long as each folder is reset to 0.
Also the Number of Shot folders will not always be consistent. Some will have B and C like the example but others may have upto D and others may not have any subshots.
Finally the Tiff folders won’t have the same name as the folders and I don’t need them to (Sorry poor image example)

I’m more than happy to share my automator workflow if anybody would like to see my weird logic workaround if that would help to explain better what i’m after or if people just want to have a giggle!

Thanks for your help.

Browser: Safari 600.3.18
Operating System: Mac OS X (10.8)

Hi Jonhol. Welcome to MacScripter.

Just to clarify things to make your query easier to answer:

  1. Are you saying that the input to the script will be folder “AAAAAA”?
  2. Do you want all the “Tiff” folders in the heirarchy to be handled or just those which are two levels down in folders “BBBBBB”, “CCCCCC”, and possibly others similarly named?
  3. Are “AAAAAA”, “BBBBBB”, etc. actual names or just markers for your illustration?
  4. Do the existing numbers in the tiff names correspond to the order in which the files were created? (This isn’t critical. It just gives an idea of the options available for the sorting process.)

Hi Nigel, sorry for the vagueness, here are the answers to your questions.

  1. The input to the script will be the folder “AAAAAA”
  2. I want all the “Tiff” folders
  3. “AAAAAA”, “BBBBBBB” etc are just markers for illustration, the actual folder names will be a unique name
  4. The existing numbers correspond to the order that the Raw file was shot rather than when the Tiff was created/processed.
    So I need to sort by creation date (well that’s what I’m doing in Automator!)

Thanks again for any help, and I hope that all makes a little more sense.

Hi Jonhol.

I forgot to ask how you wanted the root folder supplied to the script (‘choose’ dialog? Droplet? Current Finder selection?) or if you had a large number of files to rename. I’ve used a ‘choose’ dialog for convenience, but that’s easily changed. I’ve also assumed a modest number of files. Using the Finder for the task as below is simple to script, but can be quite slow if you have many hundreds of files. It’s assumed that the files in the “Tiff” folders all conform to the naming convention you described.

-- Choose the root folder.
set root to (choose folder)

tell application "Finder"
	-- Get all the "Tiff" folders in the chosen hierarchy.
	set tiffFolders to (every folder of entire contents of root whose name is "Tiff") as alias list
	-- Handle each folder in turn.
	repeat with thisFolder in tiffFolders
		-- Get all the files in this folder, sorted by creation date.
		set theseFiles to (sort files of thisFolder by creation date)
		-- Handle the files in creation date order.
		repeat with i from 1 to (count theseFiles)
			-- Get each file and its current name.
			set thisFile to item i of theseFiles
			set oldName to thisFile's name
			-- Assuming the name begins with 3 digits and ends with "_X.tif", cut the first 3 characters and replace the "X" with a zero-based index number.
			set newName to text 4 thru -6 of oldName & (i - 1) & ".tif"
			-- Apply the new name to the file.
			set thisFile's name to newName
		end repeat
	end repeat
end tell

Hi Nigel

I’ll be running it within automator and the folder will be a variable. I’ve got it to work with a tweak of your code, which is probably not the cleanest way to call the folder!

Thank you for your code. Along with rsync you have managed to shrink my automator script enormously, and make it more flexible.

Here is a jpg of the workflow to show you how big it used to be

Now it nice and manageable.

The blue highlighted section has been replaced by rsync and the red is now your code!

Although now, I’ve decided to make the workflow more complex by removing as much user interaction as possible.
So I’m now creating new folders based upon the current folders location, whereas previously a user had to specify where they would like them.
So there is a lot of working out parent directories, and then looking inside and copying file names and then creating new folders, and trying to work with volumes. Which means a lot more automator!

Thank you again.