• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: recursive folder count
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: recursive folder count


  • Subject: Re: recursive folder count
  • From: Christopher Nebel <email@hidden>
  • Date: Fri, 29 Feb 2008 21:27:20 -0800

On Feb 28, 2008, at 10:06 PM, Peter Baxter wrote:

Try these two scripts on any folder with other folders in it. I'm not talking about how much slower the AS script is. The result is different!
1.
set sourceFolder to choose folder
tell application "Finder"
set filecount to (count (get entire contents of sourceFolder))
display dialog "There are: " & filecount & " files"
end tell


2.
set target_folder to choose folder with prompt "Choose target folder to clean"
set filecount to do shell script "find " & (quoted form of POSIX path of target_folder) & " | wc -l"
display dialog "There are:" & filecount & " files"

The shell version shown above doesn't count just files, it counts folders, too. For just files, add "-type f", like this:


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

However, as other folk have observed, you'll probably get a different answer than Finder gives you, because find(1) and Finder have different ideas about what's counts as a "file" and what has contents that should be recursed into. find(1) is brain-dead simple: if it's a file in the file system, it counts; if it's a directory in the file system, it recurses. Finder, on the other hand, is more discerning: it won't count invisible files (such as the .DS_Store files that hold Finder window preferences), and it won't recurse into packages (such as applications). Generally you want the Finder's view, since it matches what the user sees.

"But," you protest, "the Finder version is so slow!" Yes, that's because you asked it to do a bunch of extra work. Notice that "get" in the "count" command? You're not asking Finder to count all those items, you're asking it to get specifiers for all of them, return those, and then AppleScript will count the resulting list. Just remove the "get" (you can ditch the parentheses, too, though they don't hurt anything), and it'll be nearly as fast as the shell.


--Chris Nebel AppleScript Engineering

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: recursive folder count
      • From: Skeeve <email@hidden>
    • Re: recursive folder count
      • From: Skeeve <email@hidden>
References: 
 >recursive folder count (From: Tim Piper <email@hidden>)
 >Re: recursive folder count (From: Skeeve <email@hidden>)
 >Re: recursive folder count (From: "Mark J. Reed" <email@hidden>)
 >Re: recursive folder count (From: Peter Baxter <email@hidden>)

  • Prev by Date: Re: Is there a shell tool to find folders greater than a given size?
  • Next by Date: Re: recursive folder count
  • Previous by thread: Re: recursive folder count
  • Next by thread: Re: recursive folder count
  • Index(es):
    • Date
    • Thread