Counting file type in a folder

I’ve been cobbling together a script that will count the amount of EPS files in a folder, then write this along with the date & time to a text file as a kind of log.

Here is what I have so far, can anyone help me at all? This is counting every single file in the folder (including sub folders which I don’t need). And I also can’t figure out how to narrow it down to a specific file type.

tell application "Finder"
	set theFolder to (path to desktop as Unicode text) & "Completed"
	set theList to do shell script "find " & (quoted form of POSIX path of theFolder) & " -type f | wc -l"
end tell

set filePath to (path to desktop as string) & "EPS Log"

try
	set fileRef to open for access file filePath with write permission
	set newText to ((current date) as string) & tab & "Total" & space & theList & return
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try

Hi,

for a single folder level you can use System Events


tell application "System Events"
	set theList to POSIX path of files of folder "Completed" of desktop folder whose kind is "Encapsulated PostScript"
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set theList to theList as text
set text item delimiters to TID

set filePath to (path to desktop as text) & "EPS Log"

try
	set fileRef to open for access file filePath with write permission
	set newText to ((current date) as string) & tab & "Total" & space & theList & return
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try


Thanks Stefan, that does the job but it just creates a list of the files in the log rather than just the actual number of files which is all I need. Can it be tweaked to do that?

Even much easier:


tell application "System Events" to set numberOfEPSFiles to count (get files of folder "Completed" of desktop folder whose kind is "Encapsulated PostScript")

set filePath to (path to desktop as text) & "EPS Log"

try
	set fileRef to open for access file filePath with write permission
	set newText to ((current date) as string) & tab & "Total" & space & numberOfEPSFiles & return
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try


That’s perfect, thanks so much! Really appreciate it.

Hello StefanK

I know that I am boring with this point but the kind property is localized. So, just for instance, for a French user your script will always return zero.
Here, “Encapsulated PostScript” is spelled “PostScript encapsulé”.
It’s time to forget kind and use type identifier which is “com.adobe.encapsulated-postscript” worldwide for eps files.

Yvan KOENIG (VALLAURIS, France) lundi 13 octobre 2014 17:57:16

Have you ever see that our host, MacScripter is always unaware of the availability of OS X 10.9 ?