1

Let’s say I have multiple files I want to delete, and they’re stored one per line in a files.txt file. I can delete them with

set deleteThis to paragraphs of (read "files.txt")

repeat with lineFromFile in deleteThis
    set fileName to POSIX file lineFromFile
    tell application "Finder" to delete file fileName
end repeat

This is the logic you usually find online — deleting them one by one, but not only does this screw your “Put Back” option (you have to z multiple times), it also plays the trash sound a bunch of times in a row.

Is there a way to delete multiple files at once, say via storing them in an array and deleting that?

Edit

The answer by @adayzdone is the best so far, but it fails on directories

0

3 Answers 3

2

Try:

    set deleteThis to paragraphs of (read "files.txt" as «class utf8»)
    set deleteList to {}

   tell application "Finder"
    repeat with aPath in deleteThis
        try
            set end of deleteList to (file aPath)
        on error
            try
                set end of deleteList to (folder aPath)
            end try
        end try
    end repeat

    delete deleteList
end tell
Sign up to request clarification or add additional context in comments.

4 Comments

It seems to only work with files whose names consist of only ascii characters, though. Do you know how to solve that?
You need to make sure that files.txt is UTF8 encoded if you want to reference files with non-ASCII file names.
It is UTF8 encoded, and I’ve tried the cat command. Still can’t get it to work.
Found another limitation — it does not work on directories. Is there a way to make it work for both directories and files?
0

Yes, Finder will take a list of paths as an argument to the delete command.

set deleteThis to paragraphs of (read "files.txt")

repeat with i from 1 to (count deleteThis)
    set item i of deleteThis to POSIX file (item i of deleteThis)
end repeat

tell application "Finder"
    delete deleteThis
end tell

Alternatively, if you're able to store the file paths as HFS paths instead of POSIX paths, you can skip the coercion:

set deleteThis to paragraphs of (read "files.txt")

tell application "Finder"
    delete deleteThis
end tell

2 Comments

Doesn’t work. It gives me an error on the line set item i of deleteThis to POSIX file (item i of deleteThis), stating error "Can’t get POSIX file \"\"." number -1728 from POSIX file ""
Sounds like you have a trailing empty line in your text file. You may need to run some cleanup in the repeat loop to make sure you're only getting valid file paths.
0
tell application "Finder"
    set file_list to entire contents of (choose folder with prompt "Please select directory.")
    move file_list to trash
end tell

3 Comments

This assumes I want to delete all the files inside a specific directory, which is not the case. I want to delete files specified in a list.
You can create file_list. above code is example of move file_list to trash
But that’s just saying “yes, you can do what you suggested”. I was looking as to how to accomplish it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.