1 Reply Latest reply: Dec 5, 2012 11:30 AM by Camelot
emanMOTU Level 1 Level 1 (0 points)

I need to write an apple script/automator program that will help me sort files into folders based on lists.

 

So I have a list of around 60 folders and the names of over 1200 corresponding files that need to go into those 60 folders.  I'm not sure the best way to approach this and don't care if it is the best I just need it to work.  The file names have no distiquishing names so I must use the lists that I have to match the files with the correct folders.

 

I know this is basic programming but I cannot figure out how to get the corresponding items out of my lists to set varriables with them. What is the best format to start with? CSV? Text File?  

 

Right now I have a CSV file where Column 1 is File Name and Column 2 is Folder Name

 

 

Here is what I have so far:

 

Automator - Get Contents of Text Edit Document - (CSV file) "Names"

 

AppleScript - 

on run {input, parameters}

set X to X + 1

set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ("," & return)

set variable1 to text item X (where variable1 is file name and X is variable)

set X to X + 1

Set PathV2 to text item X (where PathV2 is subfolder name)

set AppleScript's text item delimiters to tempTID
return input

 

Automator - Find Finder Items "variable1"

Automator - Move Finder Items "PathV2"

Automator - Loop

 

 

Can anyone help my code?  or write this better?


Automator, Mac OS X (10.6.8)
  • 1. Re: AppleScript - move files to specific folders based on list of files and folders?, AppleScript - move files to specific folders based on list of files and folders?
    Camelot Level 8 Level 8 (45,790 points)

    This doesn't sound like a big problem. It can all be done in straight AppleScript. Try something like:

     

    set base_folder to "path:to:base" -- this is the top of your directory tree

     

    set textFileContents to (read file (choose file with prompt "Select the list file"))

     

    set fileList to paragraphs of textFileContents

    set oldDelims to my text item delimiters

    set my text item delimiters to ","

     

    repeat with eachFile in fileList -- process each line

              set filename to text item 1 of eachFile

              set dirName to text item 2 of eachFile

     

              tell application "Finder"

      move (file filename of (folder base_folder)) to (folder dirName of folder base_folder)

              end tell

    end repeat

     

    The concept here is that the script first asks for the data file that lists the filename/folder combinations in CSV format. It then breaks that file data into paragraphs, and divides each paragraph into two variables based on the comma.

     

    For each paragraph it extracts the filename and directory name parameters, then tells the Finder to move the corresponding file to the corresponding subdirectory name.

     

    There's no error checking here, so missing fields in the CSV file, or missing files or subdirectories will throw an error, but those are addressable issues if you encounter them.