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

Copy files from one folder to another folder using a text file list

I am trying to copy files that match a list of file names in a text file from one folder to another.


This is what I have so far. The problem is that the event times out. What am I missing and can someone help?


set listFile to POSIX path of (choose file)

set dataList to paragraphs of (read listFile as «class utf8»)


tell application "Finder"

set ImageFile to "Macintosh HD:Users:Myuser:Pictures:Pictures 1"

set theFolder to "Macintosh HD:Users:Myuser:Pictures:Images"

repeat with thisFileName in dataList

duplicate (files of folder ImageFile whose name begins with thisFileName) to theFolder

end repeat

end tell

iMac, OS X Yosemite (10.10.3)

Posted on Jun 28, 2015 3:09 AM

Reply
31 replies

Jun 28, 2015 6:20 AM in response to Rocleaner

Thank you for the advice.


Here is the post again.


I am trying to copy files that match a list of file names in a text file from one folder to another.


This is what I have so far. The problem is that the event times out. What am I missing and can someone help?



set listFile to POSIX path of (choose file)

set dataList to paragraphs of (read listFile as «class utf8»)


tell application "Finder"

set ImageFile to "Macintosh HD:Users:Myuser:Pictures:Pictures 1"

set theFolder to "Macintosh HD:Users:Myuser:Pictures:Images"

repeat with thisFileName in dataList

duplicate (filesoffolderImageFilewhosenamebegins withthisFileName) totheFolder

end repeat

end tell

Jun 28, 2015 6:32 AM in response to Rocleaner

Does this cure the time out for you?


set listFile to POSIX path of (choose file)

set dataList to paragraphs of (read listFile as «class utf8»)


tell application "Finder"

set ImageFile to "Macintosh HD:Users:Myuser:Pictures:Pictures 1"

set theFolder to "Macintosh HD:Users:Myuser:Pictures:Images"

repeat with thisFileName in dataList

with timeout of 1200 seconds

duplicate (files of folder ImageFile whose name begins with thisFileName) to theFolder

end timeout

end repeat

end tell

Jun 28, 2015 7:06 AM in response to Rocleaner

In the results pane the process gets to this point which is using the name of the first file in the list. It then just spins away until it times out.


tell application "Finder"


duplicate every file of folder "Macintosh HD:Users:Myuser:Pictures:Pictures 1" whose name starts with "_DSC09092.jpg" to "Macintosh HD:Users:Myuser:Pictures:Images"

Jun 28, 2015 8:42 AM in response to Rocleaner

Been testing this locally. Added trailing ':' to HFS path folder names. Added replacing and exact copy booleans to avoid AS complaining about file already existing when it did not, and then exact copy retains the original ownership/permission bits.


I tested mine with nine files, and yours may be hundreds or thousands — a factor that would take some time, and you may have to extend that timeout value. The code as shown, does not hang in my testing.


set listFile to POSIX path of (choose file)

set dataList to paragraphs of (read listFile as «class utf8»)


tell application "Finder"

set ImageFile to "Macintosh HD:Users:Myuser:Test 1:"

set theFolder to "Macintosh HD:Users:Myuser:Images:"


repeat with thisFileName in dataList

with timeout of 1200 seconds

duplicate (files of folder ImageFile whose name starts with (thisFileName as text)) to theFolder with replacing and exact copy

end timeout

end repeat

end tell

Jun 28, 2015 10:11 AM in response to Rocleaner

Try this bash script wrapped in Automator:


User uploaded file


The Run Shell Script Action is:


if [ ! -d "$HOME/Pictures/Images" ]; then

mkdir "$HOME/Pictures/Images"

fi

while read line

do

if [ -f "$HOME/Pictures/Pictures 1/$line" ]; then

cp "$HOME/Pictures/Pictures 1/$line" "$HOME/Pictures/Images"

fi

done < "$1"


(Note: The first 3 lines test if the Folder exists, and if not, creates it. You can delete this if you want.)

Jun 28, 2015 9:41 PM in response to Rocleaner

The likeliest scenario I can see that could cause your script to fail is when there are many (thousands?) of files in the source directory, and the fact you are asking the Finder to scan every file to see if it matches the text file entry.


What's not clear from your post is whether you need a stub match (file whose name begins with...) vs. an exact match (file whose name is...). The latter will execute much much faster since the Finder can get the file directly and not have to scan every single file to see if its name starts with the search string.


Since your example shows the file name "_DSC09092.jpg", I have to think that changing the line to '... whose name is thisFileName' could fix your problem.

Jun 29, 2015 4:29 AM in response to Camelot

Camelot wrote:


The likeliest scenario I can see that could cause your script to fail is when there are many (thousands?) of files in the source directory, and the fact you are asking the Finder to scan every file to see if it matches the text file entry.



If that's the case, then the OP is better off using bash.

Jun 29, 2015 8:39 AM in response to VikingOSX

Thank you VikingOSX, TonyT1 and Camelot. I appreciate your help. I tried Vikings latest suggestion but I still have the timeout with not a single file being transferred. But the reason, it seems is the size of the source file. More than 32,000 files to choose from! I made a smaller source file with just 3 files and it works. Very quickly! Especially with Camelot's suggested file name.


I guess the task is just to big for this method. The source is around 32,000 as mentioned and the text file has about 9,500 file names. These files are exact and include extensions.


I have tried Tony T1's solution but I am new to Automator. (Even newer than to AppleScript!) I have tested it in Automator but it does not produce any results. I deleted the Image folder and it created that so that part works. The rest executes something and the script ends with no errors but also without any result.


Thanks so much so far. I would appreciate some further guidance to perhaps get Tony T1's solution to work. Perhaps I am just missing something really simple in the running of the script.


Or are the other solutions?

Jun 29, 2015 9:14 AM in response to Rocleaner

Rocleaner wrote:


The source is around 32,000 as mentioned and the text file has about 9,500 file names. These files are exact and include extensions.


I would appreciate some further guidance to perhaps get Tony T1's solution to work. Perhaps I am just missing something really simple in the running of the script.


Lets debug what the script I posted is doing by using echo


Change:

cp "$HOME/Pictures/Pictures 1/$line" "$HOME/Pictures/Images"

to

echo cp "$HOME/Pictures/Pictures 1/$line" "$HOME/Pictures/Images" >> $HOME/Desktop/debug.txt


This will create a text file on the Desktop called debug.txt if matching files are found


If a debug.txt file is not created, then add the following line after the do command (before the if command):

echo $HOME/Pictures/Pictures 1/$line" >> $HOME/Desktop/debug.txt


Post a few lines of what's created in debug.txt


BTW, the problem could be the text file with the names — I'm assuming that this file is a plain text file. (if it's not, it can easily be converted to plain text)

What program did you use to create the text file with the names?

Jun 29, 2015 9:49 AM in response to Tony T1

The changes to the script do not produce the debug file. Adding the line you suggest after the "do" command produces this error: -: -c: line 9: unexpected EOF while looking for matching '"'


I am not sure if I am running Automator correctly. Should I be running it from inside Automator? When I start the run I get a warning: "This folder action will not receive input when run inside Automator. To test this folder action within Automator, add the “Get Specified Finder Items” action to the beginning of your workflow. Remove or disable the action before running the workflow outside of Automator."

I am not sure how to respond to this. Should I be running it from outside Automator and if so how do I do it?


The text file was produced from an Excel file. File/Save As/ and I think I chose Windows Formatted. Hope this helps and thank you again.


I have to sign out now as its 00:45 in my part of the world... Back tomorrow....

Jun 29, 2015 10:30 AM in response to Rocleaner

Rocleaner wrote:


The changes to the script do not produce the debug file. Adding the line you suggest after the "do" command produces this error: -: -c: line 9: unexpected EOF while looking for matching '"'


My mistake, missing quote,

use: echo "$HOME/Pictures/Pictures 1/$line" >> $HOME/Desktop/debug.txt



Rocleaner wrote:


the text file was produced from an Excel file. File/Save As/ and I think I chose Windows Formatted.



That's the problem. Windows uses a CRLF, and UNIX, LF

This can be easily converted in Automator with tr in an additional Run Shell Script Action

This now passes the array of names to the last Run Shell Script Action, so no need to read from the file

(note the revised Run Shell Script Action)


User uploaded file


New Run Shell Script Action:


tr -d "\r" < "$1"


Final (revised) Run Shell Script Action:


if [ ! -d "$HOME/Pictures/Images" ]; then

mkdir "$HOME/Pictures/Images"

fi

for f in "$@"

do

if [ -f "$HOME/Pictures/Pictures 1/$f" ]; then

cp "$HOME/Pictures/Pictures 1/$f" "$HOME/Pictures/Images"

fi

done

Jun 29, 2015 10:31 AM in response to Rocleaner

32,000 files in one folder is going to cause delays for the Finder, especially if you are scanning every file to see if its name matches.


Changing to '... whose filename is...' will certainly be quicker than '... whose filename starts with...', but there is an even faster way.


Instead of:


duplicate (files of folder ImageFile whose name starts with (thisFileName as text)) to theFolder with replacingand exact copy


Try:



duplicate (filethisFileName of folderImageFile) totheFolder with replacing and exact copy


This will duplicate the file directly, without having to do a directory scan for relevant files.

Copy files from one folder to another folder using a text file list

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