Skip navigation

Get Sum of folder contents after a repeat function

469 Views 7 Replies Latest reply: Aug 12, 2013 7:50 AM by Bernard Harte RSS
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
Aug 6, 2013 7:33 AM

This is my script and I can count the nember of files in a folder.

 

but how can i separate out the folders,

so that i get each result out but also as sum total.

 

set dtF to paragraphs of (do shell script "ls -F ~/Desktop | grep '/' | cut -d'/' -f1")

set tc to (count dtF)

 

 

repeat with i from 1 to tc

          set folderName to item i of dtF --<:  is the folder name, no need to use text item delimiters -->":"

          if folderName does not start with "2_" and folderName does not start with "Hot" and folderName does not start with "Pre" and folderName does not start with "Design" and folderName does not start with "Keywords" and folderName does not start with "Se" and folderName does not start with "Su" and folderName does not start with "Den" and folderName does not start with "Mi" then

 

                    tell application "Finder"

                              set numberOfFiles to (count of (files in folder folderName))

 

                    end tell

 

          end if

end repeat


iMac, Mac OS X (10.6.8)
  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Aug 10, 2013 8:52 AM (in response to MattJayC)

    If I understand what you want, you might like to try adding an empty list item at the top of the script:

     

         set folderCount to {}

     

    then, you can append your results to this by changing:

     

         set numberOfFiles to (count of (files in folder folderName))

     

    to:

     

         set end of folderCount to {fName:folderName, fCount:count of (files in folder folderName)}

     

    that will leave you with a list of lists containing pairs with the folder name and folder count.

     

    It's then a trivial task to loop through the list to get the total number of files:

     

         set totalFiles to 0

         repeat with theFolder in folderCount

                   set totalFiles to totalFiles + (fCount of theFolder)

         end repeat

     

    (the variable totalFiles will contain the number you are looking for).


     



  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Aug 12, 2013 5:13 AM (in response to MattJayC)

    I think it might just be that you need fName of theFolder instead of folderName.

     

    display dialog "Total Files " & totalFiles as text & return & fName of theFolder & tab & (fcount of theFolder) as text

  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Aug 12, 2013 7:43 AM (in response to MattJayC)

    Well, yes, but you have to reference each item (of folderCount)'s fName and fCount in the list in the display dialog, so you need to collect those values as you go through the loop:

     

    set up an empty variable near the top of the script:

     

    set dialogString to ""

     

    then add this into the repeat...end repeat loop:

     

    set dialogString to dialogString & return & theFolder's fName & " " & theFolder's fCount

     

    when you get to the display dialog, change it to:

     

    display dialog "Total Files " & totalFiles as text & dialogString

  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Aug 12, 2013 7:50 AM (in response to MattJayC)

    Good.  Glad to help.

     

    I should have said, the alternative to doing it this way would be to have something like the following in the display dialog:

     

    display dialog "Total Files " & totalfiles & fCount of item 1 of folderCount & " " & fName of item 1 of folderCount & return & item 2 of folderCount & " " & fName of item 2 of folderCount & return ...

     

    As you can see, there are two problems with that approach:

     

    1.  You don't necessarily know how many folders you are dealing with when you write the statement

    2.  It's a lot of work that the repeat loop deals with for you.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.