PDA

View Full Version : File count on different chosen directories




nellbern
Apr 9, 2012, 03:05 PM
I have been trying for days to make this to work. I need to able to have a pdf file count of the chosen directories in the server.

folder 1 has total of 24 pdfs
folder 2 has 10 pdfs
folder 3 has 234 pdfs

If I select folder 1 & folder 2 I would to see these results

folder 1 24 pdf
folder 2 10 pdf


set target_folder to choose folder with prompt "Choose target folder to count files" with multiple selections allowed without invisibles

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


This what I have so far but I'm getting an error:\"Can’t make quoted form of POSIX path of {alias \"Macintosh HD:Users:user:Desktop:Stuff:folder 2 :\", alias \"Macintosh HD:Users:user:Desktop:Stuff:folder1:\"} into type Unicode text." number -1700 from quoted form of POSIX path of {alias "Macintosh HD:Users:user:Desktop:Stuff:folder 2 :", alias "Macintosh HD:Users:user:Desktop:Stuff:folder1:"} to Unicode text

I was trying using only applescript but scripting the finder takes too long to give me results



chown33
Apr 9, 2012, 04:14 PM
Since you're allowing multiple selections, the result will be a list (a.k.a. an array). That's what the { } means in the error message: it's showing you that you have a list, and it contains two alias objects.

The solution is just like any other list of things: you apply the action to each item in the array. The AppleScript way to do this is a repeat loop like:
repeat with thing in listOfThings
-- code for one thing goes here
end
You should be able to find numerous examples of repeat with loops on the web, or in any AppleScript tutorial that covers looping.

As a test, I suggest writing a repeat loop that simply shows an alert with each item in the array. This will show the loop working. Then you can change it to run the shell script.

kryten2
Apr 10, 2012, 12:14 AM
global files_list_count_total
set target_folder to choose folder with prompt "Choose target folders to count files" with multiple selections allowed without invisibles
set files_list_count_total to 0
repeat with i from 1 to the count of target_folder
set this_item to item i of target_folder
set the item_info to info for this_item
if (alias of the item_info is false) and (folder of the item_info is true) then
process_item(this_item)
end if
end repeat

-- this sub-routine processes folders
on process_item(this_item)
-- NOTE that the variable this_item is a folder reference in alias format
-- FOLDER PROCESSING STATEMENTS GOES HERE
set this_item to this_item as string
tell application "Finder"
set files_list to (items of folder this_item whose name extension is "pdf") as alias list
end tell
set files_list_count to count files_list
set files_list_count_total to files_list_count_total + files_list_count
end process_item
display dialog "Total “PDF“ Files: " & files_list_count_total & return & "Folders: " & i with title "Pdf file count" buttons {"OK"} default button 1


If you want to use do shell script use this handler :

on process_item(this_item)
-- NOTE that the variable this_item is a folder reference in alias format
-- FOLDER PROCESSING STATEMENTS GOES HERE
set files_list to ((do shell script "find " & quoted form of POSIX path of this_item & " -type f -name *.pdf | wc -l") as integer)
set files_list_count_total to files_list_count_total + files_list
end process_item

nellbern
Apr 10, 2012, 08:03 AM
Thank You chown33 for the explanation & kryten2 for the applescript. Have a wonderful day