Count contents of folders, subfolders

I’m trying to find a way to have a script count the files (not folders) of a certain folder. It also searches sub folders and sub folders withing the original folder.

I’m trying to figure out a way to code this beast, but sadly have run into brick walls.

I’m sure there are better ways of doing this but this works:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Here’s another way…

http://scriptbuilders.net

[This script was automatically tagged for color coded syntax by Script to Markup Code]

Hi all :slight_smile:
You can also try this (only for Os X, if you are on Os9, then it is necessary to use a recursive syntax like that proposed by Jon) :

tell application "Finder" to return (count (files of (entire contents of (choose folder))))

:wink:

A faster method (taking advange of the UNIX underpinnigns) found be to use the find command in conjunction with wc. Pass something similar to the following using the do shell script directive:

find . -not -type d | wc -l

Find from the current directory all files which are not of type directory, then count the number found. This is also expandable to finding files matching a pattern.

Why does the script stop in its tracks after this command?
I wanted to add a display dialog to show the file count.
Does a return command halt a script?

return when placed in a on run handler will return out of the script. If you want to do as you ask try something like this instead (if you want to keep it in one line)

tell application "Finder" to display dialog (count (files of (entire contents of (choose folder))))

This is a bit faster, James :wink:

display dialog (count (paragraphs of (do shell script "find " & quoted form of POSIX path of (choose folder) & " -type f ! -name '.*'")))

scnr

This works:

tell application "Finder" to display dialog ¬
(count (files of (entire contents of (choose folder)))) ¬
buttons "OK" default button "OK"

… but why doesn’t this work?:

tell application "Finder" to display dialog ¬
("Number of files (including in subfolders) is " + ¬
(count (files of (entire contents of (choose folder)))) as string) + "." ¬
buttons "OK" default button "OK"

AppleScript Error: Can’t make "Number of files (including in subfolders) is " into type number.

Hi,

+ adds numbers
& concatenates strings

Voila!
I’m an idiot!
A big thanks & tip o’ the hat to StefanK!

“&” concatinates lists too.


set Alist to {1, 2, 3}
set Blist to {4, 5, 6}
set Clist to Alist & Blist --> {1, 2, 3, 4, 5, 6}
-- set Dlist to Alist + Blist fails.
-- but
set Dlist to Alist
set Dlist's end to Blist --> {1, 2, 3, {4, 5, 6}}

When I want to add them up, this works:


set Alist to {1, 2, 3}
set Blist to {4, 5, 6}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "+"
set C to (run script (Alist as string)) + (run script (Blist as string))
set AppleScript's text item delimiters to tid
C --> 21

[Addendum]
Doing the list sum this way, however, is over 100 times faster than the one above.


set Alist to {1, 2, 3}
set Blist to {4, 5, 6}
set C2 to Alist & Blist
set N to 0
repeat with I in C2
	set N to N + I
end repeat

OK, here’s my final droplet. Got it like I want, even to confirm the name of the folder dropped onto it:

on open theFolder
	tell application "Finder" to display dialog ¬
		(("Number of files (including in subfolders) in the folder, \"" & ¬
			name of (info for theFolder)) & ",\" is " & ¬
			(count (files of (entire contents of (theFolder as alias))))) & "." ¬
			buttons "OK" default button "OK"
end open

Two general questions, though:

  1. When parsing the script text, why does Script Editor sometimes move around where I’ve placed the soft return character (¬)? In this script, it does it before my “buttons” qualifier. I put it there for clarity; why can’t Script Editor leave it alone?

  2. When I get error sheets in Script Editor, the sheet comes down for an instant, then disappears before I have time to read it. I have to repeatedly run the error-prone script to see it enough times to read it completely. Does it happen to you? Mac OSuX 10.4.9, Script Editor 2.1.1.

Generally, the Script Editor will not let you split up a keyword phrase as the compiler interprets it.