Create folders based on filenames and then populate folders?

Here’s the situation: Every morning I have a folder of files (DV video) consisting of a customer number and “asset number” (12345-01, 29386-04, etc.) that have been batch exported out of FCP overnight. Each customer can have several “assets.”

I want to learn to write a script that will create a new folder for each customer number and then populate the folder with the assets of the same name. The folder should be just “12345” and filled with “12345-01,12345-02…” The customer numbers change every day and the number of assets also varies.

The actual exported files have a much longer name, but as a newbie, I cobbled together a renamer in Automator that gets the files to the correct naming convention. I am grateful for any insights or solutions that would solve my issue and help me learn the method along the way!

Model: Dual 2 GHz PowerPC G5
AppleScript: 2.1.2
Browser: Firefox
Operating System: Mac OS X (10.4)

I don’t understand you very well. Please clarify.

  • So first of all, you receive in a folder some files OR in that folder a folder with files in it (and no other folders with files).
    "/Folder With Received Files/myFCPMovies/" (where * can only be a file)
    "/Folder With Received Files/myFCPMovies/Movie 1/
    (where * can only be a file)

  • Second you go to a file to see which customer has send it.
    15465-MOVIEID-45A654D (this files belongs to customer 15465

  • Then you check in your database-folder if the folder “15465” all ready exists. If not, make a new one.

  • Move all the files of the folders in “/Folder With Received Files/” to the folder in the database-folder that belongs to the right customer (in this example “15465”)

  • Delete all remaining folders in “/Folder WIth Received Files/”

Am I right?


Thank you for the reply, here is some clarification.

“/Folder With Received Files/myFCPMovies/*” (where * can only be a file)

This is my starting point every morning. I would like to create a folder inside /myFCPMovies/ for each unique customer number (12345-xx) and then move the FILES with that same 5-digit prefix into the newly created folder.

BEFORE: /myFCPMovies/*12345-01,*12345-02,*12345-03,*9876-01,*9876-02,*9876-03…

AFTER: /myFCPMovies/12345/*12345-01,*12345-02,*12345-03

I’ve been studying books and forums and have made less complicated scripts, but I’m still a newbie. Here’s all I have so far regarding this project.

tell application “Finder”
set theList to every file of (choose folder)
get name of item 1 of theList
set Customer to name of item 1 of theList
make new folder at alias “StripedRAID:Users:Shared:myFCPMovies:” with properties {name:Customer}
end tell

I know I have a long way to go, so I appreciate any guidance that will help me learn better!


try this

set sourceFolder to (choose folder)
set destinationFolder to "/Volumes/StripedRAID/Users/Shared/myFCPMovies/"

tell application "Finder" to set theFiles to files of sourceFolder
repeat with aFile in theFiles
	set Nm to name of aFile
	set {TID, text item delimiters} to {text item delimiters, "-"}
	set customer to text item 1 of Nm
	set text item delimiters to TID
	set dest to quoted form of (destinationFolder & customer & "/" & Nm)
	do shell script "/bin/mkdir -p " & quoted form of (destinationFolder & customer)
	do shell script "/bin/mv " & quoted form of POSIX path of (aFile as text) & space & dest
end repeat

Thank you my friend! It worked! I’ll enjoy learning the process through reverse-engineering and studying your technique. Hopefully someday soon I will also be posting solutions for helpless newbs!