I am trying to write script that will go through a folder checking subfolders for files, if one contains files it will get me the path to those subfolders and the quantity of files in them. If the subfolder doesn’t contain files but other folders it should move on and check those for files. If it doesn’t contain anything it should just ignore it.
As I currently have it, the script requires me to select the folder one level above the subfolders I want to check. Ideally I would like to be able to select the top level folder and it will go through all subfolders until it finds some which contain files as mentioned.
This is the script I have currently, which puts the results into TextEdit, as I said at the moment it only checks the folders within the folder I have selected and goes no further. I got a lot of it from a book, which I modified to suit but now I am stuck. Any help would be greatly appreciated!
tell application "Finder"
set theFolder to (choose folder)
set these_items to (get every folder of theFolder)
set these_names to {}
repeat with i from 1 to count of these_items
set qty to the count of document files of (item i of these_items)
set the end of these_names to the POSIX path of (item i of these_items as alias) & qty
end repeat
end tell
set AppleScript's text item delimiters to return
set the item_list to these_names as string
set AppleScript's text item delimiters to {""}
tell application "TextEdit"
activate
make new document
set text of document 1 to the item_list
end tell
property these_names : {}
set theFolder to (choose folder)
tell application "Finder" to set theFolders to folders of theFolder
processFolders(theFolders)
set {TID, text item delimiters} to {text item delimiters, return}
set the item_list to these_names as text
set text item delimiters to TID
tell application "TextEdit"
activate
make new document
set text of document 1 to the item_list
end tell
on processFolders(folderList)
repeat with aFolder in folderList
tell application "Finder"
set documentCount to count document files of aFolder
set subFolders to (every folder of aFolder)
end tell
if documentCount > 0 then set end of these_names to (documentCount as text) & tab & POSIX path of (aFolder as alias)
if (count subFolders) > 0 then processFolders(subFolders)
end repeat
end processFolders
‘these_names’ being a persistent property, it should be reset to {} on every run. Doing it at the top of the script will be clearer to read; doing it at the end will cause less data to be saved back into the script file.
Hello, can you tell me how to reset the these_names property please so each time the script is run I don’t have the previous results included in the text file.
I wanted to tweak the script slightly so instead of getting the count of files within the folder I could get each filename with the full path. The following works slightly, it gives me the filenames within each subfolder, but they all appear in one long string, as in the path to that subfolder and then all the filenames one after each other.
Is there a simple way so that each file will have its own path, on its own line in the text file?
Thanks again.
property these_names : {}
set these_names to {}
set theFolder to (choose folder)
tell application "Finder" to set theFolders to folders of theFolder
processFolders(theFolders)
set {TID, text item delimiters} to {text item delimiters, return}
set the item_list to these_names as text
set text item delimiters to TID
tell application "TextEdit"
activate
make new document
set text of document 1 to the item_list
end tell
on processFolders(folderList)
repeat with aFolder in folderList
tell application "Finder"
set documentCount to count document files of aFolder
set fileName to name of document files of aFolder
set subFolders to (every folder of aFolder)
end tell
if documentCount > 0 then set end of these_names to POSIX path of (aFolder as alias) & fileName
if (count subFolders) > 0 then processFolders(subFolders)
end repeat
end processFolders
on processFolders(folderList)
repeat with aFolder in folderList
tell application "Finder"
set documentFiles to document files of aFolder
set subFolders to (every folder of aFolder)
end tell
repeat with aFile in documentFiles
set end of these_names to POSIX path of (aFile as alias)
end repeat
if (count subFolders) > 0 then processFolders(subFolders)
end repeat
end processFolders