Batch-create folders with names drawn from a csv file

Hello,

I have a FileMaker file containing 500 customer records.
I also have a couple thousands of pictures I've taken with each of the customers that are, very unfortunately, dumped in one folder.

So, what I'd like to do is to be able to batch-create 500 new folders, one for each customer, named after the person. (so I can go though the pics and manually move them to corresponding folders.)

After 5 minutes of searching for a software that might take care of the "batch-creation of folders + renaming based on a csv file" part, I found a half dozen of such applications, but alas, all of them were for windows only.

Is there one for mac users? Or can this be done by AppleScript??

Thank you in advance for any tips you may have.

MacBook1,1, Mac OS X (10.6.4)

Posted on Jul 16, 2010 8:19 PM

Reply
2 replies

Jul 16, 2010 9:35 PM in response to tokyo_dynamite

There are probably a few utilities out there, but an AppleScript or Automator workflow can be used. Assuming just the name in each record in the csv text file, an AppleScript to do this would be:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
on run -- make new folders from names in a text file
(*
makes new folders at the destinationFolder using names from the inputFile text file
if useExistingFolders is set to false, a number suffix is added to the name if the folder exists
the colon (:) is illegal for Finder names, but can be used to specify subfolders
*)
set useExistingFolders to false -- use folders that already exist?

set inputFile to missing value -- set the source text file path here
try
set inputFile to inputFile as alias
on error
set inputFile to (choose file with prompt "Choose a text file containing folder names:")
end try

set destinationFolder to missing value -- set the destination folder path here
try
set destinationFolder to destinationFolder as alias
on error
set destinationFolder to (choose folder with prompt "Choose a destination folder for the new sub folders:")
end try

repeat with anItem in paragraphs of (read inputFile)
set anItem to anItem as text
if anItem is not "" then -- skip blank items
set targetFolder to destinationFolder
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {nameList, AppleScript's text item delimiters} to {text items of anItem, tempTID}

repeat with theName in nameList -- deal with sub folders
if not useExistingFolders then set theName to (getUniqueName for theName from targetFolder) -- avoid duplicate names
try
tell application "Finder"
make new folder at targetFolder with properties {name:theName}
set targetFolder to (the result as alias)
end tell
on error number -48 -- folder already exists
set targetFolder to ((targetFolder as text) & theName) as alias
end try
end repeat
end if
end repeat

end run


to getUniqueName for someName from someFolder
(*
check if someName exists in someFolder, creating a new unique name if needed
parameters - someName [text]: a name.extension to check for
someFolder [mixed]: a folder to check
returns [text]: a unique name
*)
set {counter, divider} to {"00", "_"}

set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1
set theName to text 1 thru here of someName
if here is -1 then -- no extension
set theExtension to ""
else
set theExtension to text (here + 1) thru -1 of someName
end if

set newName to theName & theExtension
tell application "System Events" to tell (get name of items of folder (someFolder as text))
repeat while it contains newName
set counter to text -2 thru -1 of ((100 + counter + 1) as text) -- leading zero
set newName to theName & divider & counter & theExtension
end repeat
end tell

return newName
end getUniqueName
</pre>

For an Automator solution, I have a Make New Folders from Text Items action that basically does the same thing. An example workflow would be:
1) *Ask for Finder Items* -- get the text file(s)
2) *Combine Text Files* -- get the text
3) *Filter Paragraphs* { Return paragraphs that are not empty }
4) *Make New Folders from Text Items*

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Batch-create folders with names drawn from a csv file

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