Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Count all files and subfolder files within a folder Desktop
As strange as it seems, there is no simple way in the Finder to count all files in a folder, including the files in the subfolders. Some solutions were discussed in an earlier macosxhints article, but there is also a straightforward way to do this using AppleScript.

Copy and paste this script into Script Editor, and save it as an Application. Then just drag and drop a folder onto the icon, and you'll get a file count. You can also download the completed droplet from its VersionTracker page.

[robg adds: This works, but be aware that the total presented does not include the subfolders themselves -- it just counts the files themselves.]
    •    
  • Currently 3.17 / 5
  You rated: 4 / 5 (6 votes cast)
 
[24,972 views]  

Count all files and subfolder files within a folder | 12 comments | Create New Account
Click here to return to the 'Count all files and subfolder files within a folder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Count all files and subfolder files within a folder
Authored by: Anonymous on Nov 09, '04 11:37:00AM

To count al files, subfolder files and subfolders themselves you just have to open the cointaining folder in a new window in list view and open all levels clicking on the arrows. The total of files subfolder files and subfolders is displayed on the bottom bar of the window.

If there is a lot of branching sub folders CMD-ALT-Click on the arrows expands all levels of the structure (expect low refresh rates for large amounts of files).

You can do this from a folder that contains the actual folder which contents you want to count, but remember that the bottom counter registers all items in the window including the target folder and any other items present.


---
Its impossible to create a foolproof device because fools are very ingenious people.



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: Trunkmonkey on Nov 09, '04 04:50:51PM

How about:

dyn-012:~/Movies sean$ find . | wc -l
98
dyn-012:~/Movies sean$

Just files:

dyn-012:~/Movies sean$ find . -type f | wc -l
94
dyn-012:~/Movies sean$

Just directories:

dyn-012:~/Movies sean$ find . -type d | wc -l
4
dyn-012:~/Movies sean$



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: joshewah on Nov 09, '04 05:03:24PM

was about to post this, guess im too slow!



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: DeltaTee on Nov 09, '04 11:46:40AM

If you are running under OSX a much faster version would use a combo of cd, find (or ls), and wc.

The applescript only method would be fairly slow, but would be good for applying a more complex function to all found files.



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: brettmjohnson on Nov 09, '04 02:13:23PM

It is, indeed trivial from the shell:

#!/bin/sh
# Specify the root of the tree as first parameter. Current directory is default.
echo "Total Apps: " `ls -aRF1 $1 | egrep '.*\.app/$' | wc -l`
echo "Total Files: " `ls -aRF1 $1 | egrep -v '^$|.*:$|.*/$' | wc -l`
echo "Total Folders: " `ls -aRF1 $1 | egrep -v '^\.{1,2}/$' | egrep '.*/$' | wc -l`

You could speed it up by writing the output of ls -aRF1 to a temp file and grepping through that temp file for the rest of the jobs.



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: kenahoo on Nov 10, '04 07:23:34PM

An easier and faster way is to use 'find':

#!/bin/sh
# Specify the root of the tree as first parameter.
echo "Total Apps: " `find $1 -name '*.app' | wc -l`
echo "Total Files: " `find $1 -not -type d | wc -l`
echo "Total Folders: " `find $1 -type d | wc -l`

-Ken



[ Reply to This | # ]
Counts Folders & Files
Authored by: cfoster on Nov 09, '04 12:18:27PM

Here's a slightly optimized version that counts apps, files and folders:

-- To treat Appliccation bundles as Applications, use the first FOLDER_TYPE.
-- To treat them as folders (and parse their contents), use the second.

property FOLDER_TYPES : "Folder Volume"
--property FOLDER_TYPES : "Application Folder Volume"

global anzahlApps
global anzahlFiles
global anzahlFolders

on run
	set theFileList to (choose folder) as list -- Select a folder
	open theFileList -- then just treat it like a drag & drop
end run

on open theFileList
	
	set anzahlApps to 0
	set anzahlFiles to 0
	set anzahlFolders to 0
	
	repeat with one_item in theFileList
		File_or_Folder(one_item)
	end repeat
	
	activate me
	display dialog "CountEverest says:" & return & ¬
		"   Total Apps:    " & anzahlApps & return & ¬
		"   Total Files:     " & anzahlFiles & return & ¬
		"   Total Folders: " & anzahlFolders ¬
		buttons {"OK"} default button {"OK"} giving up after 20
	
end open


on File_or_Folder(one_item)
	
	tell application "Finder" to set item_kind to the kind of one_item
	
	if (FOLDER_TYPES contains item_kind) then -- is it some kind of folder?
		
		tell application "Finder" to set folder_contents to every item of one_item -- does it have contents?
		if (item_kind is "Application" and folder_contents is equal to {}) then
			set anzahlApps to anzahlApps + 1 -- It's a non-bundle Application
		else
			set anzahlFolders to anzahlFolders + 1 -- It's a Volume, Folder, or Application bundle
		end if
		
		repeat with one_item in folder_contents
			my File_or_Folder(one_item as alias)
		end repeat
		
	else if (item_kind is equal to "Application") then -- only execute when Apps are not folders.
		set anzahlApps to anzahlApps + 1
	else
		set anzahlFiles to anzahlFiles + 1 -- everything else is considered a document.
	end if
	
end File_or_Folder

Note: Applications Bundles are counted as folders



[ Reply to This | # ]
Counts Folders & Files
Authored by: cfoster on Nov 09, '04 12:25:50PM

Whoops. That last note about application bundles is wrong. They are treated as just apps with the current property selection.



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: DanFrakes on Nov 09, '04 01:55:59PM
I'm a fan of CalculateSizeCM:
Contextual menu plugin for MacOS X that calculate the total size and number of sub folders, files, invisible items of the selected files and folders. It can also calculate the break down by fork (data/resource) and can show more info than Finder's Get Info dialog.


[ Reply to This | # ]
Erhm...
Authored by: rbrockerhoff on Nov 09, '04 03:49:03PM
...if I may plug my XRay utility here...
It does count files and subfolders, invisible items, and can optionally look into application packages. Besides doing lots of other things, of course ;-)

[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: johnsawyercjs on Nov 09, '04 04:26:47PM

Non-programmers could also install the shareware utility "Super Get Info", which allows you to specify a keyboard command that you can use after selecting a folder in the Finder, to display a Get Info window that shows the total number of files and folders inside that folder, as well as other info the Finder's Get Info doesn't show. This utility also lets you select more than one item in the Finder, and do a Get Info on it, to bring up several Get Info windows, one for each item you select, as pre- OS X Finders did, instead of the OS X Finder's method of adding up the Get Info figures for all the items you select and displaying them lumped together in one window. Super Get Info's default keyboard command is Command-Option-I, but it lets you change this to the Finder's Command-I if you wish.



[ Reply to This | # ]
Count all files and subfolder files within a folder
Authored by: robJ on Nov 10, '04 09:59:05AM
This basic script appears to work on my 10.3.5 system. It should present the same counts that Finder reports when all disclosure triangles are open in a window. It does not count invisible files or items within packages. I don't recommend using this on an entire hard drive although it did not choke/time out on either of mine (internal: 73709 files, 11719 folders; external: 54134 files, 9327 folders).
 set root_fol to (choose folder)

 tell application "Finder"
  set files_ to count files of entire contents of root_fol
  set folders_ to count folders of entire contents of root_fol
 end tell

 display dialog ("Path: " & POSIX path of root_fol) & return & return & ¬
  "Files: " & files_ & return & "Sub-folders: " & folders_ & return & ¬
  "Total Item Count: " & (files_ + folders_) buttons ¬
  {"OK"} default button 1
-- Rob

[ Reply to This | # ]