Create folder structure based on filenames, move files inside

Hey guys,

So I’ve found a script that can create folders based on a bunch of quicktimes and then put those quicktimes inside the newly created folders. But I can’t figure out how to do a few other things.

Basically I do VFX work from home, clients send me a gang of quicktime movies, and I want to automate them into a shot folder.

So I would have a folder full of quicktime movies…I want to run a script that generates one folder for each quicktime in the directory. That new folder is named after the quicktime. Inside that new folder, there would be a set of subfolders created. Inside one of the subfolders would be the quicktime movie.

So Example

Quicktime name: HTG101_100_001.mov

Would create the following folders:

Top folder: HTG101_100_001

Inside would be subfolders with items in them including further subfolders.

I could type it all out… but It would be easier to give you an existing blank folder with the structure.

Once all the folders are created, the source quicktime movie, would put itself a few levels deep inside the subfolders in the plates directory.

So far I found this, which makes the folder named after the quicktime, moving the quicktime inside, but I can’t figure out how to build all the sub directories, and have the quicktime reside inside those. Does that make sense?


tell application “Finder”

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

set x to mlist

repeat with i from 1 to the count of x

    set this_file to item i of x

    if i is not 1 then

        set previous_file to item (i - 1) of x

        set prev_ext to cur_ext

        set prev_name to new_name

    else

        set prev_name to ""

    end if

    set cur_ext to name extension of this_file

    set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

    if new_name is not equal to prev_name then

        set new_folder to make new folder with properties {name:new_name} at current_folder

        move this_file to new_folder

    else

        move this_file to new_folder

    end if

end repeat

end tell


Model: Mac Pro
AppleScript: 2.5
Browser: Safari 537.36
Operating System: Mac OS X (10.12.2)

As a starting point you may use :

tell application "Finder"
	
	set selected to selection
	
	set current_folder to item 1 of selected
	
	set mlist to every file of current_folder
	
	set x to mlist
	
	repeat with i from 1 to the count of x
		
		set this_file to item i of x
		
		if i is not 1 then
			
			set previous_file to item (i - 1) of x
			
			set prev_ext to cur_ext
			
			set prev_name to new_name
			
		else
			
			set prev_name to ""
			
		end if
		
		set cur_ext to name extension of this_file
		
		set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)
		
		if new_name is not equal to prev_name then
			
			set new_folder to make new folder with properties {name:new_name} at current_folder
			set subfolderLevel1 to make new folder at new_folder with properties {name:"level 1"}
			set subfolderLevel2 to make new folder at subfolderLevel1 with properties {name:"level 2"}
		end if
		move this_file to new_folder
		# Add instructions moving wanted files in subfolders
		
	end repeat
	
end tell

When I ran it I got the arborescence :
HTG101_100_001
HTG101_100_001.mov
level 1
level 2
HTG101_100_025
HTG101_100_025.mov
level 1
level 2

If it really does what you want, I will edit it to no longer use the Finder which I hate.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 26 juillet 2017 11:31:40

Okay, this is awesome, so far!!!

So basically, the way it is set up now it is creating level 1 and level 2 folders.

Given this example I would need the quicktime movie itself to be moved inside the level 2 folder.

If that works, I’d also need additional folders created to match the folder structure, but dang if this isn’t 99% of what I need!!

For instance… in the folder structure, I’d need the quicktime movie to end up in this folder
Main_folder/plates/plate/quicktime.mov

Shot folder layout would be this:

Level 1: Main_folder: Contains subfolders: “2D, 3D, plates, renders”

 [b] Level 2[/b]: [b]2D[/b] folder contains subfolders:  "ae, mocha, nuke, ps"
 [b]3D[/b] folder contains subfolders : "cache, data, scenes, textures", and a text document called 
 workspace.mel
 [b]plates[/b] folder contains subfolders: "etc, plate, proxy"
 [b]renders[/b] folder contains subfolders: "comp, graphics, ibtw, lighting, mattes, playblasts, 
 precomp, quicktimes, temp"

      [b]Level 3[/b]: [b]ae[/b] folder contains blank text document labeled the name of the 
      quicktime, with a  _v001 before the extension. 
      [b]ps[/b] folder contains blank text document labeled the name of the quicktime, with a 
      _v001 before the extension.

One other thing, could this also be made to work with a folder of images? I don’t always have quicktime movies. In fact it’s usually numbered DPX files. Those would be in their own subfolder that would have to end up in the plates directory. But all the other functionality would be the same.

Model: Mac Pro
AppleScript: 2.5
Browser: Safari 537.36
Operating System: Mac OS X (10.12.2)

If I understood well the script below does the job.

(* http://www.macscripter.net/viewtopic.php?id=45854 *)

tell application "Finder"
	
	set selected to selection
	set current_folder to item 1 of selected
	# Build a list of the files available
	set mlist to every file of current_folder as alias list
	# Build a list of the folders available
	set theFolders to every folder of current_folder as alias list
	
	set x to mlist
	
	repeat with i from 1 to the count of x
		set this_file to item i of x
		if i is not 1 then
			set previous_file to item (i - 1) of x
			set prev_ext to cur_ext
			set prev_name to new_name
		else
			set prev_name to ""
		end if
		
		set cur_ext to name extension of this_file
		
		set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)
		
		if new_name is not equal to prev_name then
			
			set new_folder to make new folder with properties {name:new_name} at current_folder
			
			set subfolder2D to make new folder at new_folder with properties {name:"2D"}
			set subfolder2Dae to make new folder at subfolder2D with properties {name:"ae"}
			close access (open for access file ((subfolder2Dae as text) & new_name & "_v001.txt")) # Creates the blank text file
			set subfolder2Dmocha to make new folder at subfolder2D with properties {name:"mocha"}
			set subfolder2Dnuke to make new folder at subfolder2D with properties {name:"nuke"}
			set subfolder2Dps to make new folder at subfolder2D with properties {name:"ps"}
			close access (open for access file ((subfolder2Dps as text) & new_name & "_v001.txt")) # Creates the blank text file
			
			set subfolder3D to make new folder at new_folder with properties {name:"3D"}
			set subfolder3Dcache to make new folder at subfolder3D with properties {name:"cache"}
			set subfolder3Ddata to make new folder at subfolder3D with properties {name:"data"}
			set subfolder3Dscenes to make new folder at subfolder3D with properties {name:"scenes"}
			set subfolder3Dtextures to make new folder at subfolder3D with properties {name:"textures"}
			close access (open for access file ((subfolder3D as text) & "workspace.mel")) # ADDED to create the blank text file
			
			set subfolderPlates to make new folder at new_folder with properties {name:"plates"}
			set subfolderPlatesEtc to make new folder at subfolderPlates with properties {name:"etc"}
			set subfolderPlatesPlate to make new folder at subfolderPlates with properties {name:"plate"}
			set subfolderPlatesProxy to make new folder at subfolderPlates with properties {name:"proxy"}
			
			set subfolderRenders to make new folder at new_folder with properties {name:"renders"}
			set subfolderRendersComp to make new folder at subfolderRenders with properties {name:"comp"}
			set subfolderRendersGraphics to make new folder at subfolderRenders with properties {name:"graphics"}
			set subfolderRendersIbtw to make new folder at subfolderRenders with properties {name:"ibtw"}
			set subfolderRendersLighting to make new folder at subfolderRenders with properties {name:"lighting"}
			set subfolderRendersMattes to make new folder at subfolderRenders with properties {name:"mattes"}
			set subfolderRendersPlayblasts to make new folder at subfolderRenders with properties {name:"playblasts"}
			set subfolderRendersPrecomp to make new folder at subfolderRenders with properties {name:"precomp"}
			set subfolderRendersQuicktimes to make new folder at subfolderRenders with properties {name:"quicktimes"}
			set subfolderRendersTemp to make new folder at subfolderRenders with properties {name:"temp"}
			
			
		end if
		--move this_file to new_folder
		# Add instructions moving wanted files in subfolders
		repeat with aFolder in theFolders
			set fName to name of aFolder
			if fName = new_name & "_bg_plt_v001" then
				try
					move aFolder to new_folder
				end try
				exit repeat
			end if
		end repeat
		
		move this_file to subfolderPlatesPlate
		
	end repeat
	
end tell

I wish to add that, minus possible changes for pictures files, I will not work more upon this question.
Doing more is not an helper task, it’s one which must be asked to a developer working to earn his life.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 28 juillet 2017 18:24:55

THIS IS UNBELIEVABLE!!!

YOU TOTALLY ROCK!!! Thank you!

For a tiny bit of clarification, DPX is the filetype. So For instance it would be a folder full of

HTG100_101_001

contents:
HTG100_101_001.0001.dpx
HTG100_101_001.0002.dpx
HTG100_101_001.0003.dpx

Etc.

One other note, would be the movement of the quicktime into the plates directory. But dang… everything else is amazing… Seriously. Thank you!

What do you charge for your services? If I need anything else in the future?

Seriously, this will save me SOOOOO much time! THANK YOU THANK YOU THANK YOU!!

Thanks for the feedback.

When I help it’s FREE.

If I think that the task is more than a simple help one, I stop helping.
As I’m retired I refuse to work for money. I know that developers must earn their lives and wish that the problem is transferrer to such being.

Some years ago I accepted to help someone but, as the exchange became long I decided to continue offlist.
What an error ! During three months everyday a new question was added when the one asked on eve was solved.
As I learnt interesting things in the process I tried to be patient but at last it was really too much and I closed the exchanges.
Now I accept no exception.

Oops, I just saw that I missed the request for a text file named “workspace.mel” in the subfolder 3D.

I edited the script and pasted it in message #4

I’m not sure of what you ment with :

Must I understand that the quicktime file must not be moved at the root of its folder but in the subfolder plates ?
If it’s that, what to do when the file is a picture one ?

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 28 juillet 2017 19:45:40

So regarding the move of the file into plates… this is probably where things get tricky. You tell me…

So HTG101_100_001.mov should end up in

HTG101_100_001/plates/plate

if it is a folder full of dpx files etc…

I would have a folder called HTG100_101_001_bg_plt_v001 from my client… (if not I would rename it to match this naming structure.)

The operators would be “bg_plt” in the name.

That folder would also have to be moved into

HTG101_100_001/plates/plate

But the tricky part would be the the main folder would still have to be named HTG100_100_001

same with the naming of the text files in ae and ps. etc.

Does that make sense?

SERIOUSLY… if you don’t want to work on it anymore I totally understand!!!

Editing the script so that the original file is moved into HTG101_100_001/plates/plate.

What is problematic is the treatment of the folder HTG100_101_001_bg_plt_v001 because, as I’m not a sooth sayer, I am unable to guess where it is stored when we call the script.
Remember, the script is aware of the location of the movie because it’s selected.
Must I understand that the selection contain the movie file AND the folder which appear now like a Jack in a Box.

QUESTION: You wrote :
HTG101_100_001.mov should end up in
…I would have a folder called HTG100_101_001_bg_plt_v001
If I read well, 101_100 in the filename became 100_101 in the folder name.
Is it the truth or is it a typo ?

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 28 juillet 2017 20:52:17

Here is what I understood from your late message.

The selected folder contain something like that:

HTG001_002_bg_plt_v001 – a folder
HTG001.mov – a movie
HTG105_001_bg_plt_v001 – a folder
HTG105.mov – a movie
HTGflyer_bg_plt_v001 – a folder
HTGflyer.dpx – a picture
HTGNick Brignola_bg_plt_v001 – a folder
HTGNick Brignola.mp4 – a movie

Here, the code is ready to treat this kind of data.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 28 juillet 2017 21:19:37

So, the folder full of DPX files would have this naming structure…

HTG100_101_001_bg_plt_v001
or
SDL100_101_001_bg_plt_v001
or
DDA100_101_001_bg_plt_v001

The part of the folder name that would name the main folder would be anything before “_bg_plt_v001”

So main folder would be called HTG100_101_001

text files would be still called HTG100_101_001_v001.txt

etc.

Entire folder and all contents of HTG100_101_001_bg_plt_v001 would be moved into
HTG101_100_001/plates/plate

So any folder which contains “bg_plt” in it’s title, acts just like a quicktime movie

So if I have a folder full of the following as an example…

HTG100_101_001_bg_plt_v001
HTG100_101_002_bg_plt_v001
HTG100_101_003_bg_plt_v001
HTG100_101_004_bg_plt_v001

There are 4 different shots here.

So these would be in shot folders called

HTG100_101_001
HTG100_101_002
HTG100_101_003
HTG100_101_004

And they would each be moved to

HTG101_100_001/plates/plate
HTG101_100_002/plates/plate
HTG101_100_003/plates/plate
HTG101_100_004/plates/plate

Does that make sense?

The folder naming is based on: HTG (show) 101 (episode number)100 (sequence number) 001 shot number.

The " _bg_plt" indicates that this folder contains a “background plate”

Does that help?

No, it’s not clear for me.

Must I understand that the structure is

selectedFolder
HTG100_101_001_bg_plt_v001
HTG100.mov
SDL100_101_001_bg_plt_v001
SDL100.mov
DDA100_101_001_bg_plt_v001
DDA100.dpx

Or is it :

selectedFolder
HTG100_101_001_bg_plt_v001
HTG100.mov
SDL100_101_001_bg_plt_v001
SDL100.mov
DDA100_101_001_bg_plt_v001
DDA100.dpx

For me it’s late. Will be back tomorrow.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 28 juillet 2017 21:45:19

did you update the script in post 4? It appears to not yet move the quicktime or do the folder.

But yes your last post sounds correct!

THANKS!

Selected folder would contain; ANY/EITHER of the following

selectedFolder
HTG100_101_001_bg_plt_v001 (a folder)
HTG100_101_001.mov (a movie)
SDL100_101_001_bg_plt_v001 (a folder)
SDL100_101_001.mov (a movie)

So any folder selected with either folders or movies or even a combination of the two, could sort them into the folder structure. With all variations ending up in the plate directory.

Does that help?

Also, I’m in California, so I know you’re 9 hours ahead of me. I appreciate you staying up as late as you have!!

FYI my family and I were just in London/Paris last month, it’s a beautiful country. We only got to spend about 8 hours in Paris after taking the Eurostar out for the day. My wife already wants to go back!

OK.

I replaced the script by a new version.
I’m not sure that it fully match your needs.

It assume that every folder available is accompanied by at least one file.
On entry it builds a list of files available and a list of folders available.
In a loop, it creates the hierarchy of subfolders required by one file
then it search for a folder whose name match the given rules.
If it find one, it move it as required
at last it moves the file itself.

So, if a folder exists but no “associated” file is available, the folder is not treated.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 29 juillet 2017 11:36:59

Hi.

Here, based on my understanding of the discussion above, is a version which creates all the folder hierarchies with a single shell script and then creates the text files and moves the original files. I’m not clear what extension’s meant in “blank text document labeled the name of the quicktime, with a _v001 before the extension.” I’ve assumed “.txt”, which is what I’d expect with a text file.

tell application "Finder"
	set selected to selection
	-- Selection assumed here to consist of just 1 folder containing movie files or folders or both, all to be treated in the same way.
	set selected_folder to item 1 of selected as alias
	-- Get the folder's items and, separately, their names.
	set original_items to every item of selected_folder
	set item_names to name of every item of selected_folder
end tell

-- Derive quoted and unquoted forms of the names, without extensions, to use for the folders.
set shot_folder_names to {}
set quoted_shot_folder_names to {}
set astid to AppleScript's text item delimiters
repeat with this_name in item_names
	-- If a name has an extension, lose the extension. Otherwise assume it's a folder name containing "_bg_plt_v" and lose everything from that point.
	if (this_name contains ".") then
		set AppleScript's text item delimiters to "."
	else
		set AppleScript's text item delimiters to "_bg_plt_v"
	end if
	set end of shot_folder_names to text 1 thru text item -2 of this_name
	set end of quoted_shot_folder_names to quoted form of result
end repeat

--- Put together a path formula representing all the required "shot folder" hierarchies.
set item_count to (count quoted_shot_folder_names)
if (item_count > 1) then
	-- More than one shot folder name. Get a text containing them all, comma-delimited and in braces.
	set AppleScript's text item delimiters to ","
	set shot_folder_name_group to "{" & quoted_shot_folder_names & "}"
else if (item_count is 1) then
	-- Only one shot folder name.
	set shot_folder_name_group to item 1 of quoted_shot_folder_names
else
	set AppleScript's text item delimiters to astid
	error "The selected folder is either empty or not a folder!"
end if
set AppleScript's text item delimiters to astid
set hierarchy_formula to quoted form of POSIX path of selected_folder & shot_folder_name_group & "/{2D/{ae,mocha,nuke,ps},3D/{cache,data,scenes,textures},plates/{etc,plate,proxy},renders/{comp,graphics,ibtw,lightning,mattes,playblasts,precomp,quicktimes,temp}}"
-- Use it to create the shot folder hierarchies.
do shell script "mkdir -p " & hierarchy_formula

-- Move stuff into each shot folder hierarchy in turn.
set selected_folder_path to selected_folder as text
repeat with i from 1 to (count shot_folder_names)
	set this_shot_folder_name to item i of shot_folder_names
	set shot_folder_path to selected_folder_path & this_shot_folder_name
	-- Create a text file in the "3D" folder.
	close access (open for access file (shot_folder_path & ":3D:workspace.mel"))
	-- And one each in the "2D:ae" and "2D:ps" folders.
	set text_file_name to this_shot_folder_name & "_v001.txt"
	close access (open for access file (shot_folder_path & ":2D:ae:" & text_file_name))
	close access (open for access file (shot_folder_path & ":2D:ps:" & text_file_name))
	-- Move the original item into the "plates:plate" folder.
	tell application "Finder" to move (item i of original_items) to folder (shot_folder_path & ":plates:plate") -- replacing yes
end repeat

PS. The hierarchy_formula string shown above stops at the right margin in my browser, but it’s complete in the “Open in this Scriplet” link.

Edit: Destination folder for original item(s) corrected in the light of Yvan’s comment below. Folders in the selected folder now handled too and in the same way as files. Single-item formula bug fixed.

Hello Nigel

I tested your proposal.

You missed a part of the task which, as it’s alas often the case, was not described at first.
Here it was introduced in message #7 and is about folders which must be moved into the newly created folders.

Beyond that, your script move the original files in a wrong location.
It moves them in this kind of location :
“SSD 500:Users:???:desktop:mov copie:HTG001_002:plates:HTG001_002.mov”
when it’s supposed to be :
“SSD 500:Users:???:desktop:mov copie:HTG001_002:plates:plate:HTG001_002.mov”

Have fun with that. :wink:

To correct that I made some changes:


# Instruction added at the very beginning
set theFolders to name of every folder of current_folder # ADDED

# Instruction edited to move in the correct location
tell application "Finder" to move (item i of mlist) to folder (path_to_hierarchy & ":plates:plate") -- replacing yes # EDITED

# some instructions moving the additional folders
tell application "Finder"
		set pass to 0
		repeat with fName in theFolders
			set fName to fName as text
			if fName = this_folder_name & "_bg_plt_v001" then
				move folder (current_folder_path & fName) to folder path_to_hierarchy
				exit repeat
			end if
		end repeat
	end tell


Other feature : In my tests, if there is a single file in the selected folder, the script fails after creating a folder whose name is enclosed between parenthesis.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 29 juillet 2017 13:46:39

Oops. Thanks, Yvan. Now corrected. I’ll have a look at the folder business this afternoon.

Edit: Folder business now fixed too — if I’ve correctly understood that any folders in the selected folder are to be treated in the same way as files apart from the derivation of the “shot folder” names. I’ve also fixed the bug Yvan discovered which occurs when there’s only one item in the selected folder.

Guys/Gals… this is EVERYTHING I wanted!!!

OMG I’m so happy!!!

Thank you thank you thank you!!!

Thanks for the feedback.
May you tell which location is the correct one for the folder.
I put it at the root of the file dedicated folder while Nigel puts it in the plate subfolder.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) dimanche 30 juillet 2017 11:32:49

The correct folder is in Plates/plate