I’m struggling with a particular part of a script and wonder if y’all can help me
I’ve written a repeat loop that runs thru a text file, grabbing each line (each line contains a reference number for a file). All well and good - however, I’ve also got a list of filenames (using entire contents of folder etc) that I want to cross reference to the text file.
How do I do this? The best way I’ve thought of so far is to check for the filename as part of the repeat loop going through the file, but I’ve been unable to work out the syntax to compare the reference from the text file with all of the files listed in the other list!
Should have explained myself a little more clearly. I am trying to write a purge program that works from a text files listing URN’s: -
123456789
987654321
etc etc. I want to be able to go through this file, grab each URN, then use the URN to search a series of folders for the actual file, then delete the file.
I’ve managed to do this now - to compare the two lists, I’ve used:
if file_name_list contains full_urn_name then
tell application “Finder”
The problem I have now is that the list of filenames I’ve gathered using “entire contents of folder” is just that - the file’s name. I need to figure out how to get the path to the file back so I can use the Finder to delete it!
tell application "Finder"
set file_name_list to entire contents of alias "path:to:target:folder:" as alias list
repeat with this_file in file_name_list
if name of this_file contains full_urn_name then delete this_file
end repeat
end tell
set fileNamesTxt to read (choose file with prompt "Select file containing names of files to delete:")
set baseFolder to choose folder with prompt "Select folder to process:"
set fileNamesList to paragraphs of fileNamesTxt
tell application "Finder"
delete (every file of entire contents of folder baseFolder whose name is in fileNamesList)
end tell
Two bug warnings:
‘entire contents’ is buggy in OS8/9 and may fail silently on complex folder structures (a safer, if slower, solution is to traverse the folders using a recursive routine)
‘paragraphs of txt’ will fail on strings containing more than approx 4000 paragraphs.