Thursday, August 21, 2014

#1 2008-12-26 11:01:53 am

BS0D
Member
Registered: 2008-12-22
Posts: 179

Count files in folders and subfolders ...

Hey everyone,

I'm trying to add a functionality to my ASS app through a checkbox, which is supposed to count all the files included in the chosen folders, including all the sub-folders, and then return the results in a non-editable text field...

All I found was this: for one thing, this script takes forever to do its job, and I cant even integrate it to my app for some reason, it always returns an error.

I was wondering if you knew of a shell script or a simple applescript that could do that simply (and that I could maybe re-use in my thingy) ?


EDIT | by the way, I do have this to make a count work, but it does NOT include subfolders ...

Applescript:



tell application "Finder"
   set numberOfFiles to count of (files in folder theFolder)
end tell

Thanks smile

Last edited by BS0D (2008-12-26 11:07:21 am)


Filed under: Finder

Offline

 

#2 2008-12-26 11:46:06 am

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

OK, I found a syntax which corresponds to my needs smile

Applescript:


                       if state of button "countFiles" of drawer "drawer" of window "main" is 1 then
                           tell application "Finder"
                               set numberOfFiles to (count (get entire contents of theFolder))
                           end tell
                           set contents of text field "fileCount" of drawer "drawer" of window "main" to numberOfFiles
                       end if

I just didn't know I could put it that way, I'm still not too familiar with the syntax. Good thing I tried it anyway !

Sorry to bother you guys, I'm kind of learning as I go -- the best way in my opinion.

At least this thread could help someone in the future ! smile


Filed under: Finder

Offline

 

#3 2008-12-26 12:00:21 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: Count files in folders and subfolders ...

This is pretty fast.

Applescript:


set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l") as integer

Cheers,

Craig

Offline

 

#4 2008-12-26 12:04:45 pm

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

Craig Williams wrote:

This is pretty fast.

Applescript:


set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l") as integer

Cheers,

Craig

Thanks Craig,

This works great too, but it probably includes hidden files in the count, as it finds more than the applescript code I just found wink

You could put it like this to only count FILES ?

Applescript:



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

Last edited by BS0D (2008-12-26 12:21:54 pm)

Offline

 

#5 2008-12-26 12:19:15 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: Count files in folders and subfolders ...

Yea, I see that as well. Sorry about that. I will let you know if I find a faster way
that does not count hidden files.

Offline

 

#6 2008-12-26 12:23:55 pm

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

Applescript:



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

This one is pretty fast actually, much faster than the "Tell Finder" method.

This suits my needs don't worry ... wink Plus it's also good to know how to count hidden files as well, you never know, I might one day wanna do that

Offline

 

#7 2008-12-26 01:29:14 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: Count files in folders and subfolders ...

When I use the command:

Applescript:


find -type f

I not only get hidden files that do not begin with a dot but also it traverses application
bundles as well.

From the tests I have done this is working well.

Applescript:


set numberOfFiles to count of (paragraphs of (do shell script "mdfind -onlyin " & quoted form of POSIX path of theFolder & " kMDItemFSInvisible == 0"))

Offline

 

#8 2008-12-26 02:25:20 pm

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

I'm trying to add another checkbox to execute a renameFile function in subfolders, but I'm not sure how to do that ... any idea?

only thing I found that could possibly work is this :

Applescript:


set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))

So I use it with conditions like this :

Applescript:


if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
                           set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))
                       else
                       set source_folder to (choose folder "Choose the files you want to rename :")
                       set search_parameter to button returned of (display dialog "Do you want to search and replace in :" buttons {"Both", "Folder Names", "File Names"} default button 3)
end if blah blah (rest of my fuction)

But this doesnt work at all. Upon building, Xcode returns an error :

Applescript Error wrote:

Expected "," but found property (-2741)

I cant find a way around that ...

Offline

 

#9 2008-12-26 02:33:25 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10480
Website

Re: Count files in folders and subfolders ...

BS0D wrote:

Applescript:



       set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))

Only the Finder and System Events know something about file and entire contents


regards

Stefan

Offline

 

#10 2008-12-26 03:19:51 pm

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

Right, so here's my script now :

Applescript:



if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
                       if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
                           tell application "Finder"
                               set source_folder to (every file of entire contents of (choose folder with prompt "Choose the files you want to rename :"))
                           end tell
                       else
                           set source_folder to (choose folder "Choose the files you want to rename :")

( THE REST OF MY FUNCTION )
end if...

But if I tick the "inFolders" checkbox, it doesnt work at all ... the application just hides !
Am I not using the conditions right or soemthing ?

Last edited by BS0D (2008-12-26 03:20:34 pm)


Filed under: Finder

Offline

 

#11 2008-12-26 04:38:58 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10480
Website

Re: Count files in folders and subfolders ...

I'd prefer

Applescript:


if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
   set source_folder to (choose folder "Choose the files you want to rename :")
   if state of button "inFolders" of drawer "drawer" of window "main" is 1 then
       tell application "Finder" to set theFiles to files of entire contents of source_folder
   else
       tell application "Finder" to set theFiles to files of source_folder
   
   
   -- (the rest of my FUNCTION)
end if


regards

Stefan

Filed under: Finder

Offline

 

#12 2008-12-27 11:24:45 am

BS0D
Member
Registered: 2008-12-22
Posts: 179

Re: Count files in folders and subfolders ...

Right, it does build now, but it still doesnt do anything within the subfolders even when the button is checked.

I tried moving the "end if" before the end of the function but in vain. I'm pretty sure I'm not using the conditions right, otherwise it's the "tell application "Finder" to set theFiles to every file of entire contents of source_folder" that just doesnt make a difference and I should prob find something better to recurse in subfolders ...


Filed under: Finder

Offline

 

#13 2008-12-27 01:51:41 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10480
Website

Re: Count files in folders and subfolders ...

I usually search for errors using log messages like

Applescript:


if state of button "renameFiles" of drawer "drawer" of window "main" is 1 then
log "button renameFiles is 1"


then you can see doubtlessly that the script has passed the line


regards

Stefan

Offline

 

#14 2013-02-14 09:19:41 am

orionrush
Member
Registered: 2008-10-05
Posts: 29

Re: Count files in folders and subfolders ...

old thread, but I caught an oversite that lead me astray - so to save others:

In reaction to a the following snippet

Applescript:

set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l")

the OP responds:
BS0D 2008-12-26 12:04:45 pm

This works great too, but it probably includes hidden files in the count, as it finds more than the applescript code I just found

As it turn out this is not returning a count for invisible files on an empty folder.
`find` outputs a path for each item in the folder minus anything leading with a dot (.)
It turns out the first path listed is the path to the folder being examined.
`wc -l` then counts the number of lines output, so even on an empty folder the the return will still be "1"

so as long as you take this onto account, this is reliable (and very fast!)


--
“In theory, theory and practice are the same. In practice, they are not.”
― Albert Einstein

Offline

 

#15 2013-02-14 09:56:08 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1884

Re: Count files in folders and subfolders ...

Using mdfind (for cached folders) is always faster than find. Also mdfind consider files the same as in the Finder. So bundles are files and not directories as in find.

From my application folder, find will tell me there are 358,866 files in there. Well if you count count the files inside the bundle they're correct in number. It also took about 10 seconds to complete. mdfind will give me 3,476 files which is the correct result because I don't want to count everything inside bundles. Also it is completed almost instantly (without the invisibles files too) instead of 10 seconds.


Kind regards

Offline

 

#16 2013-03-08 05:08:07 am

Woggledog
Member
Registered: 2012-12-06
Posts: 98

Re: Count files in folders and subfolders ...

Found this to be simple:

Applescript:

set tempfolder to "Macintosh HD:tmp"

tell application "Finder"
   {count files of entire contents of (desktop as alias), count folders of entire contents of (desktop as alias)}
end tell

tell application "Finder"
   {count files of entire contents of (tempfolder as alias), count folders of entire contents of (tempfolder as alias)}
end tell

Rather simple, I've found, but not attempted to compensate for hidden files, etc

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.036 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)