Count files in folders and subfolders ...

Hey everyone,

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

Thanks :slight_smile:

OK, I found a syntax which corresponds to my needs :slight_smile:


						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 ! :slight_smile:

This is pretty fast.


set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l") as integer

Cheers,

Craig

Thanks Craig,

This works great too, but it probably includes hidden files in the count, as it finds more than the applescript code I just found :wink:

You could put it like this to only count FILES ?



set numberOfFiles to do shell script "find " & (quoted form of POSIX path of target_folder) & " -type f | wc -l"

Yea, I see that as well. Sorry about that. I will let you know if I find a faster way
that does not count hidden files.



set numberOfFiles to do shell script "find " & (quoted form of POSIX path of target_folder) & " -type f | wc -l"

This one is pretty fast actually, much faster than the “Tell Finder” method.

This suits my needs don’t worry … :wink: Plus it’s also good to know how to count hidden files as well, you never know, I might one day wanna do that

When I use the command:


find -type f

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 :

I cant find a way around that …

Only the Finder and System Events know something about file and entire contents

Right, so here’s my script now :



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 ?

I’d prefer


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 …

I usually search for errors using log messages like


if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
   log "button renameFiles is 1"
.


then you can see doubtlessly that the script has passed the line

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.

Found this to be simple:

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