Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Creating list of folders from filenames with files organized in subfolders within

Hi all. This is my first post in the Apple Support Community and I really hope that someone can help me as I'm using apple script for the first time.


I have a list of hundreds of items that are each .jpg, .cr2, or .mov. Certain filenames have multiple extensions, ex: Q95A7170.CR2 & Q95A7170.jpg.


I'm looking for an applescript or automator action that will allow me to create a folder out of each filename with 5 subfolders in it


I need the subfolders to be called:

AUDIO

COLOR

METADATA

PICTURE

PROXY


In this instance I need all the .mov, .cr2, and .jpgs to be routed to the PICTURE folder. In later instances I'll need to route .mxf to PICTURE AND .xml .sif & .thm to METADATA.


So for example I have a list of that looks like this


User uploaded file


and I want a script that will make it look like this (i did all of this manually: creating the folders, renaming them, and putting the specific files into the picture folder)


User uploaded file

(not nearly a fraction of the list -- there are hundreds and hundreds of files)


I've been researching applescripts for hours but the only script that i could make work and was very useful was this one to create the folders out of the names of the files:

tell application "Finder"

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

repeat with this_file in mlist

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)

set new_folder to makenewfolderwith properties {name:new_name} atcurrent_folder


movethis_filetonew_folder

end repeat

end tell


Even with this script I had to manually copy all of the duplicate file named .jpgs out of the folder before it would properly run.



Also I've found these scripts but I'm not sure what information to change. Like I said, I don't know much about code at all and I'm finding more research about it is just digging me deeper in a hole that I'm not entirely ready for.


Here are the other two scripts that CLAIM they can do what I'm looking for, but I haven't gotten them to work.


1.

set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"

tell application "Finder"

set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"

repeat with theFile in theFiles

set lengthOfExtension to (length of (theFile's name extension as text)) + 1

set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)

set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}


set theContentFolder to make new folder at theFolder with properties {name:"content"}

make new folder at theContentFolder with properties {name:"archive"}

set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}

set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}

make new folder at theMetadataFolder with properties {name:"archive"}

set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}


move theFile to theContentDisplayFolder

set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"

if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder

end repeat

end tell


2.

set myFolder to "FOLDERPATH"

tell application "Finder" to set myFiles to folder myFolder's files as alias list

repeat with aFile in myFiles

tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile

set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName

do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"

tell application "System Events"

if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)

if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)

end tell

end repeat


(the .pdf, .csv extensions are examples from the person who posted that discussion.)

I tried using both of these codes but to no avail.


Overall, this process is incredibly time-consuming and this is the first time I am using apple script so I know almost nothing about code.


Also I don't know if this changes anything, but all of these files are on an external and I'm trying to save them back on this external.


PLEASE HELP! I am a night assistant editor doing very tedious work at 3:00 AM or later and anything that would expedite this process would be so unbelievably appreciated. Please, someone who's more experience with code and who's smarter than I am on this topic, help me! I know there must be a way to automate this process!


Please and so many thank yous! Really, this code would save me 40+ hours of eye-tiring work.


Shelby

Mac Pro, Mac OS X (10.6.8), 2 x 2.26 GHz Quad-Core Intel Xeon

Posted on Mar 22, 2013 12:11 AM

Reply
28 replies

Mar 22, 2013 4:02 AM in response to belbyb

Bash (aka Terminal) would be a better weapon for this kind of thing.


Make your desired directories first (AUDIO, COLOR, etc) on the external volume using Finder. For each file type you want to move, use this pattern:


$ find /Volumes/external\ Hd/ -name '*.mp3' -exec mv '{}' /Volumes/external\ HD/AUDIO


Where you replace external\ Hd with the drive name, .mp3 with the file type and AUDIO with the directory destination name.


Mar 22, 2013 8:27 AM in response to belbyb

This is how you'd do it in AppleScript. I assume you can see how to expand it to different file extensions...


set workFolder to "/path/to/folder"

tell application "System Events"


-- get files to work on

set filesToProcess to files of folder workFolder whose visible is true

repeat with thisFile in filesToProcess

set {fileName, fileExt} to {name, name extension} of thisFile



-- get name of file without extension

set rootName to text 1 thru -((length of fileExt) + 2) of fileName



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- sort files into subFolders, making sure subfolders exist

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"PICTURE"})

movethisFiletotargetSubfolder

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"METADATA"})

movethisFiletotargetSubfolder

else if fileExt is "mxf" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"PICTURE"})

movethisFiletotargetSubfolder

else


-- skip unknown file extensions

end if

end repeat

end tell


to checkForFolder({parentFolder:fParent, folderName:fName})


-- find or create a folder

tell application "System Events"

if not (exists folder fName of folder fParent) then

set output to POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell



-- returns a POSIX path

return output

end checkForFolder

Mar 22, 2013 1:45 PM in response to twtwtw

twtwtw thank you SO much for replying. Really, so helpful, so thank you. I am confused about which parts of the code to replace though. So far I've only put in my path. Do I replace the blue? The green? I'm sorry for my very basic questions, I just don't understand applescript at all and I'm finding researching it is leading me in circles. Any other detailed instructions would be so appreciated. Thank you again for responding to me.

Mar 22, 2013 1:54 PM in response to twtwtw

twtwtw, Thank you again, I'm kind of geeking out on this working. My only question is that it only creates the folder only if there are files to be placed into it. How do I ensure that each folder ALWAYS has the subfolder structure:


AUDIO

COLOR

METADATA

PICTURE

PROXY


and then subsequently these extensions are placed into the folders?


I can't thank you enough. Even this step alone is crazy helpful. But if you could help me get the folder structure to be the same in every folder, it would be out of this world.

Mar 22, 2013 3:13 PM in response to belbyb

oh, ok. delete this section of code:



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- sort files into subFolders, making sure subfolders exist

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"PICTURE"})

movethisFiletotargetSubfolder

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"METADATA"})

movethisFiletotargetSubfolder

else if fileExt is "mxf" then

set targetSubfolder to my checkForFolder({parentFolder:targetFolder, folderName:"PICTURE"})

movethisFiletotargetSubfolder

else


-- skip unknown file extensions

end if

and replace it with the following code:



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:targetFolder, folderName:thisSubfolderName})

end repeat



-- sort files into subFolders

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then

movethisFiletotargetFolder & "/PICTURE"

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

move thisFile to targetFolder & "/METADATA"

else if fileExt is "mxf" then

movethisFiletotargetFolder & "/PICTURE"

else


-- skip unknown file extensions

end if

it works exactly the same way, except that it creates all the subfolders up front. If you have trouble putting it together let me know and I'll post the whole revised script.


As I think you've figured out, the colors are just syntax highlighting: BlueBold for commands, Green for variables and function names, Black for strings and comments, BlueNoBold for keywords... All you have to do (as I should have said, but you figured out anyway) is put in the correct folder path in the first line.

Mar 22, 2013 10:02 PM in response to belbyb

Seems like you got a working answer already, but for future ref, you don't need to download Bash, it's present on your system and is the default shell when you open Terminal.app.


The elegance of twtwtw's script is truly admirable 🙂, but as I said you can do (and learn to do) this kind of thing (i.e., moving files around your computer) much simpler and easier (and with far fewer line of codes) in Bash than in AppleScript.


I didn't add the make folders function, but it's as simple as adding this line to the one I gave you before.


f=/Volumes/external\ Hd/; mkdir $f/AUDIO $f/PICTURES $f/METADATA $f/COLOR $f/PROXY


(again, where you replace external\ Hd with the name of the disk)


There are plenty of tutorials on the net for Bash, but a good book you might consider is 'Learning the Bash Shell'.

Apr 12, 2013 8:23 PM in response to twtwtw

Hey twtwtw, I'm trying to make this work on an external hard drive but I can't get it to connect to the path. I've tried both


ABC_06:LTO_104_lost_Part1:20120818-C300-01:CONTENTS:CLIPS001


and


ABC_06/LTO_104_lost_Part1/20120818-C300-01/CONTENTS/CLIPS001


ABC_06 is the name of the hard drive. The rest are the folders within. How can I get it to recognize my path to the hard drive? I was able to get it to work on my comp's internal HD but not the ext HD. Please help!


Thank you


Best,

Shelby

Apr 13, 2013 1:21 AM in response to belbyb

Posix paths use the mount point, most likely:


"/Volumes/ABC_06/LTO_104_lost_Part1/20120818-C300-01/CONTENTS/CLIPS001"


if you want to use HFS paths, then what you have is correct - they start with the volume name - but you need to to use a file or alias keyword to make the system recognize it as a path:


file "ABC_06:LTO_104_lost_Part1:20120818-C300-01:CONTENTS:CLIPS001"

Apr 16, 2013 7:18 PM in response to twtwtw

I used the new Posix path but nothing happens. It says "running" for a split second and then nothing.... Any suggestions? I'm not sure what else to do... if I can't get this code to work on the external then I'm going to have to do hours and hours of unnecessary, eye-tiring work. Please help, twtwtw. You're my code genius!


Thanks!


S

Apr 16, 2013 7:36 PM in response to twtwtw

Now I just copied one of the folders to my desktop to try routing to there and it still didn't work. I think I may have somehow corrupted the code when I was trying to customize bits of it. Mind taking a look and telling me why it's not working? I tried going off of the code that you created and adding for more extensions. Nothing is working, unfortunately. Here is the code I am using:


set workFolder to "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"

tell application "System Events"

-- get files to work on

set filesToProcess to files of folder workFolder whose visible is true

repeat with thisFile in filesToProcess

set {fileName, fileExt} to {name, name extension} of thisFile



-- get name of file without extension

set rootName to text 1 thru -((length of fileExt) + 2) of fileName



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:targetFolder, folderName:thisSubfolderName})

end repeat



-- sort files into subFolders

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then


movethisFiletotargetFolder & "/PICTURE"

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

move thisFile to targetFolder & "/METADATA"

else if fileExt is "mxf" then


movethisFiletotargetFolder & "/PICTURE"

else if fileExt is "cif" or fileExt is "cpf" then

move thisFile to targetFolder & "/METADATA"

else


-- skip unknown file extensions

end if

end repeat

end tell


to checkForFolder({parentFolder:fParent, folderName:fName})

-- find or create a folder

tell application "System Events"

if not (exists folder fName of folder fParent) then

set output to POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell


-- returns a POSIX path

return output

end checkForFolder




Thanks for your help!

Apr 16, 2013 7:36 PM in response to belbyb

SImplest thing to do (and this will seem obvious after I say it 😉) is to drag and drop the folder into the applescript editor window. If it asks tell it you want the alias (it doesn't always ask) and it should give you a perfectly formed POSIX path to the folder. Next easiest thing is to open the folder in the Finder and command-click the title-bar name: that will give you a drop-down file path that you can copy.


It's odd that the posix path I gave didn't work. Is the drive HFS formatted? Have you for some reason given it an atypical mount point?

Creating list of folders from filenames with files organized in subfolders within

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.