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

How to get finder items from a list, automator, including searching through sub-folders

I have researched existing posts and found this very helpful one:

Use Automator to read a text file and copy contents to another Folder

it refers to searching for files based off a text list, and copying them to a new folder. it works as described for me, but I need to alter it for my needs. I need it to search through a larger directory with many sub folders. The current action only looks for files in one specified folder.

My specific example/need is that I have a list of approximately 100 files in a .txt file, the image files they correspond to are spread across several dozen sub-folders, but are within 1 general folder. Meaning I don't need to search through multiple drives or networked drives, just one large parent folder and all of it's sub folders. I'd like to run automator to locate those files and copy them to a specified folder.

Any help appreciated, thanks!

iMac, iOS 10.3.1

Posted on Apr 18, 2017 10:05 AM

Reply
Question marked as Best reply

Posted on Apr 19, 2017 2:04 PM

Here is an AppleScript used in an Automator Application via the Run AppleScript action.

  • Prompts for an input folder
  • Prompts for an output folder
  • Prompts for the file containing your 100 filenames.

    Assumption: not full paths to the files, but just filename.ext.

  • Reads the filenames from item 3 into a quoted list
  • Drills down into the input folder capturing full paths of matching files in item 4.
  • Copies (duplicates) matching files to outfolder. Will replace if collision, and copy retains ownership, file permissions, etc.


Launchpad : Other : Automator

  • New : Application : Choose
  • Utilities Library : Run AppleScript (drag and drop this to the right into larger workflow window).
    • Select boiler plate contents and remove.
    • Copy/paste contents of following AppleScript code into the Run AppleScript action window

      Click the hammer icon

    • Click the right-arrow (Run) icon in the top-right toolbar to run the Automator application interactively as a test
  • Automator : File menu : Save... (you can name it copy_files, match_files, etc.) and put it on your Desktop.

    Double-click to run


AppleScript code


property defloc : (path to desktop as text) as alias

property msg1 : "Select the input folder"

property msg2 : "Select the output folder"

property msg3 : "Select the input file with filenames"


use scripting additions


set name_list to {}

set match_list to {}


try

set infolder to (choose folderwith promptmsg1default locationdefloc without invisibles, multiple selections allowed and showing package contents)

set outfolder to (choose folderwith promptmsg2default locationdefloc without invisibles, multiple selections allowed and showing package contents)

set name_file to (choose filewith promptmsg3default locationdefloc without invisibles, multiple selections allowed and showing package contents)


display dialoginfolder'sPOSIX path & return & outfolder'sPOSIX path & return & name_file'sPOSIX path

-- read names from file into quoted filename list

set name_list to read name_file as «class utf8» using delimiter linefeed


tell application "Finder"

-- get every filename in infolder hierarchy whose filename matches names in name list

set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list


-- copy the files to outfolder with exact permissions and overwrite if required

repeat with anItem in match_list

duplicateanItemtooutfolder with replacing and exact copy

end repeat

end tell

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

end try

return


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler


Snapshot of Run AppleScript action with AppleScript code

User uploaded file

2 replies
Question marked as Best reply

Apr 19, 2017 2:04 PM in response to tewasko

Here is an AppleScript used in an Automator Application via the Run AppleScript action.

  • Prompts for an input folder
  • Prompts for an output folder
  • Prompts for the file containing your 100 filenames.

    Assumption: not full paths to the files, but just filename.ext.

  • Reads the filenames from item 3 into a quoted list
  • Drills down into the input folder capturing full paths of matching files in item 4.
  • Copies (duplicates) matching files to outfolder. Will replace if collision, and copy retains ownership, file permissions, etc.


Launchpad : Other : Automator

  • New : Application : Choose
  • Utilities Library : Run AppleScript (drag and drop this to the right into larger workflow window).
    • Select boiler plate contents and remove.
    • Copy/paste contents of following AppleScript code into the Run AppleScript action window

      Click the hammer icon

    • Click the right-arrow (Run) icon in the top-right toolbar to run the Automator application interactively as a test
  • Automator : File menu : Save... (you can name it copy_files, match_files, etc.) and put it on your Desktop.

    Double-click to run


AppleScript code


property defloc : (path to desktop as text) as alias

property msg1 : "Select the input folder"

property msg2 : "Select the output folder"

property msg3 : "Select the input file with filenames"


use scripting additions


set name_list to {}

set match_list to {}


try

set infolder to (choose folderwith promptmsg1default locationdefloc without invisibles, multiple selections allowed and showing package contents)

set outfolder to (choose folderwith promptmsg2default locationdefloc without invisibles, multiple selections allowed and showing package contents)

set name_file to (choose filewith promptmsg3default locationdefloc without invisibles, multiple selections allowed and showing package contents)


display dialoginfolder'sPOSIX path & return & outfolder'sPOSIX path & return & name_file'sPOSIX path

-- read names from file into quoted filename list

set name_list to read name_file as «class utf8» using delimiter linefeed


tell application "Finder"

-- get every filename in infolder hierarchy whose filename matches names in name list

set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list


-- copy the files to outfolder with exact permissions and overwrite if required

repeat with anItem in match_list

duplicateanItemtooutfolder with replacing and exact copy

end repeat

end tell

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

end try

return


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler


Snapshot of Run AppleScript action with AppleScript code

User uploaded file

How to get finder items from a list, automator, including searching through sub-folders

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