Tom
Tom

Reputation: 1223

Mac OS Delete files from folder with name recursively

I have a main directory with projects. Each project is a folder, like myfolder/projectname. Each project has a directory called var and another directory there called cache, like myfolder/projectname/var/cache. I would like to prepare a script in Apple script to run Finder recursively through myfolder folder, find there all project folders that are like var/cache and then remove all files from those folders. For instance:

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

How to achieve this? For now I have a code like this but is not working

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

Upvotes: 0

Views: 859

Answers (1)

vadian
vadian

Reputation: 285069

This can be accomplished simpler and more efficient with the shell.

* are wildcards, the first considers all folders in myfolder the second all items in cache. quoted form is needed if the real name of myfolder contains space characters.

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

Upvotes: 2

Related Questions