Mac OS 以递归方式从文件夹中删除文件

Mac OS Delete files from folder with name recursively

我有一个包含项目的主目录。每个项目都是一个文件夹,如 myfolder/projectname。每个项目都有一个名为 var 的目录和另一个名为 cache 的目录,例如 myfolder/projectname/var/cache。我想通过 myfolder 文件夹递归地在 Apple 脚本中为 运行 Finder 准备一个脚本,找到所有类似 var/cache 的项目文件夹 然后从这些文件夹中删除所有文件。例如:

myfolder/projectname1/var/cache
myfolder/projectname2/var/cache
myfolder/projectname3/var/cache

如何实现?现在我有这样的代码,但没有用

try
    
    tell application "Finder"
        
        delete (every item of folder ("/Users/myuser/myfolder") whose name is "cache")
        
    end tell
    
on error
    
    display dialog ("Error. Couldn't Move the File") buttons {"OK"}
    
end try

使用 shell 可以更简单、更高效地完成此操作。

* 是通配符,第一个考虑 myfolder 中的所有文件夹,第二个考虑 cache 中的所有项目。如果 myfolder 的真实姓名包含 space 个字符,则需要 quoted form

set baseFolder to POSIX path of (path to home folder) & "myfolder/"
do shell script "rm " & quoted form of baseFolder & "*/var/cache/*"