使用applescript删除多个文件 [英] Delete multiple files with applescript

查看:63
本文介绍了使用applescript删除多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有多个文件要删除,它们每行一个存储在 files.txt 文件中.我可以用

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

这是你通常在网上找到的逻辑——一个一个地删除它们,但这不仅会破坏你的放回"选项(你必须z多次),它还会连续播放一堆垃圾声音.

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?

编辑

@adayzdone 的答案是迄今为止最好的,但它在目录上失败

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

推荐答案

尝试:

    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

这篇关于使用applescript删除多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆