Move Finder files referencing a comma-delimited text file

Hi all,
I need to move a lot of files from one folder to another. The names of those files are listed in a comma-delimited text file (the log file from another script which compares one folder of files to another). What I have below doesn’t work: can anyone tell me why or suggest another scripting option?
Thanks, Alison


set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
	tell application "Finder"
		set theEPSFile to theSourceFolder & theEPSName
		move file theEPSFile to folder theDestination with replacing
	end tell
end repeat

So what actually happens? I just did a quick test and everything works on my end.

Here’s the funny thing. When I read the text file or even paste a single filename from the text file into this script, it gets the error “Finder can’t find file” on the move. line. But if I type a new text file with all the same info in it (and read that instead of trying to read the log file) it works fine. Something must be wrong with the way my other script writes the log file because this happens every time . or somehow the log file always gets corrupted. Here’s the line that writes this log in another script:

set theFileReference to open for access theFilePath2 with write permission
				write theName & "," to theFileReference starting at eof
				close access theFileReference

Is there a way to write a different kind of file than this?
Thanks for your help.
Alison

This is what the text looks like if I save the log file text as an html document and open it in my browser:

1�0�5�5�8�.�I�n�f�o�.�e�p�s�,�1�0�7�8�6�.�I�n�f�o�.�e�p�s�, (… etc)

This must be what the script sees. Instead of “10559.Info.eps” it sees it with a ? between each character … Maybe I can write in a line to remove all the ?, but I wonder why it has that & why I can’t see it in the text file or in the script.

Hi,

I assume, theFilePath2 is an alias.
If the file doesn’t exist, you get the error
Use string path instead with this syntax:


set theFilePath to "Disk:path:to:file.txt"
set theFileReference to open for access file theFilePath2 with write permission
write theName & "," to theFileReference starting at eof
close access theFileReference

The text is probably UTF-16,
try

read MytextFile as Unicode text

Thank you for your help, Stefan.
When I use “as Unicode text” and choose the original log file, the script just runs and runs without returning a result. Here’s what I wrote:

set fileContents to read (choose file with prompt "Choose a comma-delimited text file") as Unicode text

If I save the text file as a rich text file instead I get this:
{"簹牴昱屭慣屡湳楣灩??ぜ捯捯慲???捯捯 (.etc)

Is there another type of text?
Thanks,
Alison

log-files are normally UTF-8 encoded, but the ASCII character range 00-127 is the same as UTF-8
try

set fileContents to read (choose file with prompt "Choose a comma-delimited text file") as «class UTF8»

With «class UTF8» I get “Can’t get every text item of «data UTF85468657365206172652 (.etc - long long list)” error.

sorry, my fault utf8 must be lowercase

 as «class utf8»

Ok well it did return a list of all the file names, with:

set fileContents to read (choose file with prompt "Choose a comma-delimited text file") as «class utf8»
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems

But while it looks ok in Script Editor, copying and pasting theTextItems into TextEdit looks like this: “1”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, (.etc)

That must be what it really sees because later on I get the error “Finder can’t get file”

Ok. I went back and added to the script that writes the log file and rewrote the log file as utf8. Now I think I am getting the right list of file names. But something else is wrong … I get this error: “Finder got an error: Can’t make some data into the expected type.” On the line “duplicate …”. Is it the paths?

Here’s the current script

set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
	tell application "Finder"
		set theEPSFile to file (theSourceFolder & theEPSName)
		--move file theEPSFile to folder theDestination with replacing
		duplicate file theEPSFile to folder theDestination with replacing
	end tell
end repeat

Why do you use a CSV file?
This type is good for tables with fields, but not very convenient for just a list of files.
I’d prefer to separate the file names with return characters, then you can easily read the names with

set fileContents to paragraphs of (read (choose file with prompt "Choose a text file") as «class utf8»)

no further text item delimiters are required.

I guess, your problem is the carriage return at the end of the line, which is not considered in your script

Thanks, I took your suggestion about reading the paragraphs & changed the way the log file is written. I didn’t know you could use paragraphs, I just knew you couldn’t change AppleScript’s delimiters to the return, so I used the comma. (I guess I could have used quote marks when writing the log, but I thought I might use a .csv file from Excel someday - they’re easy to export & I receive a lot of Excel files). Thanks for your patience - these are my first read/write scripts.

What do you mean by:

the structure of a CSV file are paragraphs (CR, LF, or CRLF separated).
Each line contains items which are comma separated

Name[comma]Street[comma]City[comma]State[comma][return] John Doe[comma]5th avenue[comma]New York[comma]NY[comma][return]

Oh, the file I was working with wasn’t a real .csv. It’s just one long string with a comma between each file name, no spaces, no returns. I changed it though so now it is like this:

11372.Info.eps[return]
12305.Info.eps[return]
12469.Info.eps[return]
12566.Info.eps[return]
12723.Info.eps[return] … and so forth

Then this might be sufficient


set theTextItems to paragraphs of (read (choose file with prompt "Choose a text file") as «class utf8»)
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
	try
		tell application "Finder" to duplicate alias (theSourceFolder & theEPSName) to theDestination with replacing
	end try
end repeat

Thank you Stefan, that did work, and it is much more elegant! Thanks for sticking with me on this - I am SO happy I don’t have to select and drag 1,179 items by hand :smiley: and I learned some new things .

The only thing I found was that I had a blank line at the end of the text file so the script tried to copy the source folder into the destination folder as well as all the items in the list, so I added a line to prevent that & since there is the try, I gave myself an indication at the end of how many files were moved. Here’s the final one:

set theTextItems to paragraphs of (read (choose file with prompt "Choose a text file") as «class utf8»)
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
	try
		set theEPSFile to (theSourceFolder & theEPSName)
		if theEPSFile = theSourceFolder then
			display dialog "Encountered an empty item" buttons {"OK"}
		else
			tell application "Finder" to duplicate alias theEPSFile to theDestination with replacing
		end if
	end try
end repeat
tell application "Finder"
	tell folder theDestination to set theCount1 to (count of items) as string
end tell
set theCount2 to (count of theTextItems) as string
display dialog (theCount1 & " of " & theCount2 & " items copied to " & theDestination) buttons {"OK"}

Again, thank you!