Script that Counts Files & Folders in a Folder

I have a script that does this. The problem is, it counts .DS_Store files in folders that have them (folders that have been opened), throwing off the count. How can I get the count to ignore .DS_Store files? Thanks in advance.

on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item

process_item(this_item)
end repeat
end open

– this sub-routine processes folders

on process_item(this_item)
– NOTE that the variable this_item is a folder reference in alias format

tell application “Finder”
set the_folder to this_item
set num_folders to number of folders in entire contents of folder the_folder
set num_Items to number of files in entire contents of folder the_folder
display dialog “There are " & num_Items & " files, " & num_folders & " folders.”

end tell
end process_item

You can try this:


set fileCount to 0
set folderCount to 0
tell application "Finder"
	set theFileList to every item in folder theFolder
	repeat with x in theFileList
		if not character 1 of ((name of x) as string) is equal to "." then
			if kind of x is equal to "Folder" then -- "folder" depends from  the finder language
				set folderCount to folderCount + 1
			else
				set fileCount to fileCount + 1
			end if
		end if
	end repeat
end tell

display dialog "There are " & folderCount & " folders and  " & fileCount & " files in this folder"


if your language in the finder is english this will work

good luck

Thanks, with minor adaptation, it works! You da man!

Whenever I need to count items in a folder I use this:

tell application "Finder"
	choose folder with prompt "Chose Folder"
set theFolder to result
	return (count item theFolder)
end tell

This will return a count of all folders and documents (without the .DS files) when running 10.3.5.

If this does not work you might adjust the code to read as follows:

return (count item theFolder without invisibles)

The “without invisibles” command works when doing anything with a folder (counting, reading names, getting file paths, etc.)

Furthermore, if you only want to count folders you can adjust the code to read:

return (count theFolder)

This will only count the folders within the selected folder.

Hope this helps,
jON bEEBE

set {numFiles, numAlias, numFolder, numInvis} to {0, 0, 0, 0}

set theFolder to (choose folder)
set folderContents to list folder theFolder
repeat with tmpItem in folderContents
	set tmpInfo to info for file ((theFolder as string) & tmpItem)
	if visible of tmpInfo is true then
		if folder of tmpInfo is true then
			set numFolder to (numFolder + 1)
		else if alias of tmpInfo is true then
			set numAlias to (numAlias + 1)
		else
			set numFiles to (numFiles + 1)
		end if
	else
		set numInvis to (numInvis + 1)
	end if
end repeat

display dialog (("Visible Files: " & numFiles & return & "Folders: " & numFolder & return & "Aliases: " & numAlias & return & "Invisibles: " & numInvis) as string)

j

These are all good . . . I see there is more than one way to skin this cat.

Here’s what I ended up using:

on open these_items

repeat with i from 1 to the count of these_items

set this_item to item i of these_items

set the item_info to info for this_item

process_item(this_item)

end repeat

end open

on process_item(this_item)

set fileCount to 0
set folderCount to 0
tell application “Finder”
set theFileList to every item in entire contents of folder this_item
repeat with x in theFileList
if not character 1 of ((name of x) as string) is equal to “.” then
if not character 1 of ((name of x) as string) is equal to “_” then
if kind of x is equal to “Folder” then
set folderCount to folderCount + 1
else
set fileCount to fileCount + 1
end if
end if
end if
end repeat
end tell

display dialog “There are " & folderCount & " folders, " & fileCount & " files in this folder”

end process_item

Actually I have learned that this script, although it works fine on folders with small numbers of items, does not work on deeply nested folders with lots of items, such as iTunes folders.

Hello,

This script doesn’t seem to work on folders with large numbers of items. Any suggestions?

Thanks,

Dave