Iterate through folders

Hi, is there a way to iterate all subfolders inside of a specific folder with AppleScript?

MainFolder/
SubFolder_1/
SubFolder_2/
SubFolder_3/
etc...

The script suppose to go into 'SubFolder_1', do something there, go out and then go into 'subfolder_2', and s.o. The names of the subfolder have no specific name pattern (so i dont know their names).

Any examples, please?
Thanks in Advance

MacBook Pro

Posted on Jan 11, 2011 9:00 AM

Reply
4 replies

Jan 11, 2011 1:15 PM in response to VLK

Sure, the basic premise involves a recursive script that calls itself over and over again:

on run
set theFolder to (choose folder with prompt "Select the start folder")
doSomethingWith(theFolder)
end run

on doSomethingWith(aFolder)
tell application "Finder"
set subFolders to every folder of aFolder
repeat with eachFolder in subFolders
my doSomethingWith(eachFolder)
end repeat
end tell
-- rest of code to deal with the folder goes here
end doSomethingWith


So you start off at one folder chosen via choose folder. The doSomethingWith() handler then looks for every folder inside that folder and for each one, calls itself. In this way you don't need to know the name of the folder, or the depth - you'll just keep iterating through all the folders.

Jan 13, 2011 1:15 PM in response to Camelot

Thank you so much!
That is exactly what I was looking for!
Meanwhile I came up with another idea of doing that, but the way you did it seams to be easier.
Thanks!

PS:
If I could ask you one more question?..
I want to start this script without any user interaction, so I tried to replace your second line
set theFolder to (choose folder with prompt "Select the start folder")

with one of these:
set theFolder to "/Volumes/media/content/"
set theFolder to quoted form of "/Volumes/media/content/"
set theFolder to POSIX path of "/Volumes/media/content/"
set this_folder to quoted form of (the POSIX path of "/Volumes/media/content/")

but nothing seams to work... all I get is:
AppleScript Error: File '/Volumes/xplace/content/' wasn’t found.

Where did I go wrong?

Jan 13, 2011 2:58 PM in response to VLK

Where did I go wrong?


Where you went wrong is that you haven't been using AppleScript for 10 years or more 🙂

Seriously, historically Mac paths are based on colon-delimited paths, not the UNIX-based /-delimited paths. Therefore your mistake is in how you're defining the starting path. Apple have done some work in streamlining the process and making them interchangeable, but there are still gotchas, like the one you encountered.

You should find either of these work fine:

set theFolder to ":Volumes:media:content" as alias
set theFolder to "media:content" as alias

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Iterate through folders

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.