I’m trying to add a functionality to my ASS app through a checkbox, which is supposed to count all the files included in the chosen folders, including all the sub-folders, and then return the results in a non-editable text field…
All I found was this: for one thing, this script takes forever to do its job, and I cant even integrate it to my app for some reason, it always returns an error.
I was wondering if you knew of a shell script or a simple applescript that could do that simply (and that I could maybe re-use in my thingy) ?
EDIT | by the way, I do have this to make a count work, but it does NOT include subfolders …
tell application "Finder"
set numberOfFiles to count of (files in folder theFolder)
end tell
OK, I found a syntax which corresponds to my needs
if state of button "countFiles" of drawer "drawer" of window "main" is 1 then
tell application "Finder"
set numberOfFiles to (count (get entire contents of theFolder))
end tell
set contents of text field "fileCount" of drawer "drawer" of window "main" to numberOfFiles
end if
I just didn’t know I could put it that way, I’m still not too familiar with the syntax. Good thing I tried it anyway !
Sorry to bother you guys, I’m kind of learning as I go – the best way in my opinion.
At least this thread could help someone in the future !
I not only get hidden files that do not begin with a dot but also it traverses application
bundles as well.
From the tests I have done this is working well.
set numberOfFiles to count of (paragraphs of (do shell script "mdfind -onlyin " & quoted form of POSIX path of theFolder & " kMDItemFSInvisible == 0"))
I’m trying to add another checkbox to execute a renameFile function in subfolders, but I’m not sure how to do that … any idea?
only thing I found that could possibly work is this :
set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))
So I use it with conditions like this :
if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))
else
set source_folder to (choose folder "Choose the files you want to rename :")
set search_parameter to button returned of (display dialog "Do you want to search and replace in :" buttons {"Both", "Folder Names", "File Names"} default button 3)
end if blah blah (rest of my fuction)
But this doesnt work at all. Upon building, Xcode returns an error :
if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
tell application "Finder"
set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))
end tell
else
set source_folder to (choose folder "Choose the files you want to rename :")
( THE REST OF MY FUNCTION )
end if...
But if I tick the “inFolders” checkbox, it doesnt work at all … the application just hides !
Am I not using the conditions right or soemthing ?
if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
set source_folder to (choose folder "Choose the files you want to rename :")
if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
tell application "Finder" to set theFiles to files of entire contents of source_folder
else
tell application "Finder" to set theFiles to files of source_folder
-- (the rest of my FUNCTION)
end if
Right, it does build now, but it still doesnt do anything within the subfolders even when the button is checked.
I tried moving the “end if” before the end of the function but in vain. I’m pretty sure I’m not using the conditions right, otherwise it’s the “tell application “Finder” to set theFiles to every file of entire contents of source_folder” that just doesnt make a difference and I should prob find something better to recurse in subfolders …
old thread, but I caught an oversite that lead me astray - so to save others:
In reaction to a the following snippet
set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l")
the OP responds:
BS0D 2008-12-26 12:04:45 pm
As it turn out this is not returning a count for invisible files on an empty folder. find outputs a path for each item in the folder minus anything leading with a dot (.)
It turns out the first path listed is the path to the folder being examined. wc -l then counts the number of lines output, so even on an empty folder the the return will still be “1”
so as long as you take this onto account, this is reliable (and very fast!)
Using mdfind (for cached folders) is always faster than find. Also mdfind consider files the same as in the Finder. So bundles are files and not directories as in find.
From my application folder, find will tell me there are 358,866 files in there. Well if you count count the files inside the bundle they’re correct in number. It also took about 10 seconds to complete. mdfind will give me 3,476 files which is the correct result because I don’t want to count everything inside bundles. Also it is completed almost instantly (without the invisibles files too) instead of 10 seconds.
set tempfolder to "Macintosh HD:tmp"
tell application "Finder"
{count files of entire contents of (desktop as alias), count folders of entire contents of (desktop as alias)}
end tell
tell application "Finder"
{count files of entire contents of (tempfolder as alias), count folders of entire contents of (tempfolder as alias)}
end tell
Rather simple, I’ve found, but not attempted to compensate for hidden files, etc