How to calculate the number of files in nested folders>

Hello -


I want to find out or calculate the number files nested within nested folders.


I'm assuming I would use Terminal or Apple Script, but I am not familiar with writing the lines of code to make the request. I would need to know what to copy into Apple Script, and then how to operate the script. (I am a long time mac-user 20 years, and have back-ups, but I DON'T WANT TO DELETE MY PICS because I didn't know how to run the script 😟


I'm looking to get a rough estimate of the number of photos I have in my Pictures album. I'm very particular about organizing files by year and date.


I use iPhoto occasionally, but loading all my pics into iPhoto would take forever and kill my disk space (despite the huge storage on machines these days.) iPhoto does exactly what I don't want it to do, it makes duplicates all over the place. If I want to go to the original file, it is a wild goose chase to hunt it down.


Yes there are a lot of helpful features, but for someone who uses Photoshop for work, and am contantly creating, and modifying photos, iPhoto is not my choice. I haven't looked at any other image databases in a while. I used to work with Cumulus, which 'linked' to the file without having to duplicate it.


I will sometimes use Adobe Bridge, but that can get clunky. So I have a collection of ways for searching files, none of them is completely ideal.


Thanks,


Beth😝

iMac, OS X Mavericks (10.9), 27" iMac, 17" MacBookPro, iPhone 5s

Posted on Feb 3, 2014 9:55 AM

Reply
21 replies

Feb 3, 2014 2:03 PM in response to graphiclizard

In Terminal (replace ~/Documents with directory to count)


find ~/Documents/ ! -type d ! \( -name ".*" -or -name "Icon*" \) | wc -l


In Applescript:


set x to choose folder with prompt "Choose Folder to Count Files"

set filecount to do shell script "find " & POSIX path of x & " ! -type d ! \\( -name \".*\" -or -name \"Icon*\" \\) | wc -l"

display dialog "File Count:" & filecount

Feb 3, 2014 2:25 PM in response to Tony T1

This Applesctipt will add comma's to the file count:

(Copy into Applescript Editor, and Save as File Format: App, then just click the App to run)


set x to choose folder with prompt "Choose Folder to Count Files" default location alias (the path to pictures folder as text)

set filecount to do shell script "find " & POSIX path of x & " ! -type d ! \\( -name \".*\" -or -name \"Icon*\" \\) | wc -l"

display dialog "File Count: " & comma_delimit(trim(filecount))


on trim(someText)

repeat until someText does not start with " "

set someText to text 2 thru -1 of someText

end repeat


repeat until someText does not end with " "

set someText to text 1 thru -2 of someText

end repeat


return someText

end trim


on comma_delimit(this_number)

set this_number to this_number as string

if this_number contains "E" then set this_number to number_to_text(this_number)

set the num_length to the length of this_number

set the this_number to (the reverse of every character of this_number) as string

set the new_num to ""

repeat with i from 1 to the num_length

if i is the num_length or (i mod 3) is not 0 then

set the new_num to (character i of this_number & the new_num) as string

else

set the new_num to ("," & character i of this_number & the new_num) as string

end if

end repeat

return the new_num

end comma_delimit

Feb 3, 2014 4:33 PM in response to Pierre L.

Yes, that's better, but might as well give the OP a dialog box with the count so that it could be saved as an App:


set theFolder to choose folder with prompt "Choose Folder to Count Files" default location alias (the path to pictures folder as text)

tell application "Finder"

set filecount to count files in the entire contents of theFolder

end tell

display dialog "File Count: " & comma_delimit(filecount)


on comma_delimit(this_number)

set this_number to this_number as string

if this_number contains "E" then set this_number to number_to_text(this_number)

set the num_length to the length of this_number

set the this_number to (the reverse of every character of this_number) as string

set the new_num to ""

repeat with i from 1 to the num_length

if i is the num_length or (i mod 3) is not 0 then

set the new_num to (character i of this_number & the new_num) as string

else

set the new_num to ("," & character i of this_number & the new_num) as string

end if

end repeat

return the new_num

end comma_delimit

Feb 4, 2014 10:57 AM in response to graphiclizard

Here is a comma delimited AppleScript handler that uses Ruby as its engine. AppleScript wants to change Ruby quotes and \ characters, so these need an extra \ escape. What you see below is the Ruby code before AppleScript reformats it with additional newlines and tabs.


set value1 to "123456789.50" as string

set value2 to "123456789" as string


display dialog commify(value1) & return & commify(value2)



on commify(nbr)


if nbr contains "." then

set rb to "num = ARGV.join(\"\").to_f\n

snum = sprintf('%0.2f', num).gsub(/(\\d)(?=\\d{3}+\\.)/, '\\1,')\n

puts snum"

else

set rb to "num = ARGV.join(\"\").to_i\n

snum = sprintf('%d', num).gsub(/(\\d)(?=\\d{3}+$)/, '\\1,')\n

puts snum"

end if

do shell script "/usr/bin/ruby -e " & rb's quoted form & space & nbr


end commify

Feb 4, 2014 12:21 PM in response to graphiclizard

1 - Install tree - see install instructions


2 - Copy the following to Terminal:


cd folder-of-interest; tree | tail -1


3 - Hit return key


Typical output:


44 directories, 453 files



Install instructions:


  1. Download tree from: ftp://mama.indstate.edu/linux/tree/
  2. Decompress file
  3. Edit Makefile as instructed by INSTALL file
  4. Type cd followed by a space in Terminal
  5. Drag downloaded tree folder to Terminal window
  6. Type make in Terminal
  7. Hit return key
  8. Type sudo make install in Terminal
  9. Hit return key
  10. Supply admin password
  11. Hit return key
  12. Use tree as above


Allow sufficient time for each Terminal instruction to complete.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to calculate the number of files in nested folders>

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.