Create folder structure based on filenames, move files inside

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

Thanks for the feedback.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 2 aout 2017 10:14:32

I don’t suppose someone could wrap this up into a droplet, so I can just drag the folder full of files onto it?

Save as an application.

on open dropped_items
	-- Examine each of the one or more dropped items in turn.
	repeat with dropped_folder in dropped_items
		-- Is this dropped item actually a folder?
		tell application "Finder" to set is_Folder to (class of item (dropped_folder as text) is folder)
		
		-- Act only if it is.
		if (is_Folder) then
			tell application "Finder"
				-- Get the folder's items and, separately, their names.
				set original_items to every item of dropped_folder
				set item_names to name of every item of dropped_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 dropped_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 dropped_folder_path to dropped_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 dropped_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
		end if
	end repeat
end open

Hey guys,

So Everything is absolutely perfect!

I’ve been using the script successfully for a little while now with no complaints.

I did find one operational thing that I didn’t consider, and wondered if there’s an easy way to change one small thing.

I’m using another script inside AE that references the specific folder structure that you’ve built for me. But I made an error when I asked for the name of the text file inside the 2d/ps folder and 2d/ae folder. In other words, your work is perfect… I’m the idiot that didn’t ask for the right thing!!

The text file needs to be named with “_main_comp” in it’s title.

So current results create the following: gon102_006_005_v001.txt

but it needs to add “_main_comp” so it should read gon102_006_005_main_cmp_v001.txt

Would it be possible to alter the existing script to include this?

I tried figuring out how to alter it in the existing script, but that part’s not in plain english… and is sadly way outside my level of comprehension.

I’d appreciate any help you guys could offer!

Thanks!