Script to count files by extension

peavine,

Thank you very much.

Your script certainly does what I am looking for.

It could be a way to name the text file?

The hidden files are a problem, the resulting txt report is not clean.

The following will prompt for a file name–do not include the file extension. I don’t know any simple way to omit the hidden files but I will give that some thought.


set theSourceFolder to (choose folder with prompt "Select an HDD or folder:")
set theSourceFolder to quoted form of POSIX path of theSourceFolder

set textFile to choose file name with prompt "Choose file name" default name "zCARPA"
set textFile to quoted form of (POSIX path of textFile & ".txt")

do shell script "find " & theSourceFolder & " -type f | sed -n " & quoted form of "s/..*\\.//p" & " | sort | uniq -c > " & textFile

Very much obliged for your kind help.

thank you.

.
I found the command line to omit the hidden files from the below link and it works:
https://www.linuxquestions.org/questions/linux-general-1/how-to-tell-find-to-not-search-inside-hidden-folders-208169/

find /Users/mac/Documents/zCARPA \( ! -regex '.*/\..*' \) -type f | sed -n 's/..*\.//p' | sort | uniq -c > /Users/mac/Desktop/zCARPA.txt

Model: mac mini 2011
AppleScript: 2.6.1
Browser: Safari 537.36
Operating System: macOS 10.9

.
Also, I found the command line for total files. But I do not know how to implement it in the code.

find /Users/mac/Documents/zCARPA \( ! -regex '.*/\..*' \) -type f | wc -l > /Users/mac/Desktop/zCARPA.txt

External HDD disks?

This problem can be solved successfully with plain AppleScript using recursive search and System Events.

But this algorithm is time-consuming, therefore it is better to use AppleScriptObjC here, for maximum speed and reliability of the results. In addition, AppleScriptObjC has a simple method for filtering unnecessary content, such as hidden files.

I will try to publish a similar script, but now I need to go to work.

Yes, I will use the script for external HDDs.

Thank you very much for your help.

I incorporated the above command line in my script and it does appear to work. Getting the quoting correct was a real bear. Anyways, my script remains a kludge and KniazidisR’s AppleScriptObjC version will be the one to use.

set theSourceFolder to (choose folder with prompt "Select an HDD or folder:")
set theSourceFolder to quoted form of POSIX path of theSourceFolder

set textFile to choose file name with prompt "Choose file name" default name "zCARPA"
set textFile to quoted form of (POSIX path of textFile & ".txt")

do shell script "find " & theSourceFolder & " \\( ! -regex " & quoted form of ".*/\\..*" & " \\) -type f | sed -n " & quoted form of "s/..*\\.//p" & " | sort | uniq -c > " & textFile

This is an AppleScriptObjC solution. It assumes every item with an extension is a file, which may include some folders, depending on how you name things. But it skips invisible items, as well as items within package files. And it sorts the list by count.

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set theSourceFolder to (choose folder with prompt "Select an HDD or folder:")
-- get all files
set fileManager to current application's NSFileManager's defaultManager()
set theOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants) + (get current application's NSDirectoryEnumerationSkipsHiddenFiles)
set theFiles to (fileManager's enumeratorAtURL:theSourceFolder includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value))'s allObjects()
-- get just extensions
set theExtensions to theFiles's valueForKey:"pathExtension"
-- remove empty/missing extensions
set theFilter to current application's NSPredicate's predicateWithFormat:"SELF != ''"
set theExtensions to theExtensions's filteredArrayUsingPredicate:theFilter
-- make counted set, which does counting for us
set theSet to current application's NSCountedSet's setWithArray:theExtensions
-- build array of records so we can sort
set theResults to current application's NSMutableArray's array()
repeat with aValue in theSet's allObjects()
	(theResults's addObject:{theCount:theSet's countForObject:aValue, theValue:((theSet's countForObject:aValue) as text) & "," & (aValue as text)})
end repeat
-- get total count
set theCount to theResults's valueForKeyPath:"@sum.theCount"
-- sort list by count
set sortDesc to current application's NSSortDescriptor's sortDescriptorWithKey:"theCount" ascending:false
theResults's sortUsingDescriptors:{sortDesc}
-- create text and save
set theText to (((theResults's valueForKey:"theValue")'s componentsJoinedByString:linefeed) as text) & linefeed & (theCount as integer as text) & ",Total"
-- write file to desktop
set deskPath to path to desktop as text
set fileRef to (open for access ((deskPath & "Results.csv") as «class furl») with write permission)
set eof fileRef to 0
write theText to fileRef as «class utf8»
close access fileRef

I tried a couple of your versions but they seem to be having trouble with subfolders – it’s like they’re treating the whole subpath as an extension.

.
Thank you very much for the AppleScriptObjC solution.

I am using Mavericks 10.9.5
Versión 2.6.1 (152.1)
AppleScript 2.3.2

mac mini 2011

Is there any way to make it run on my system?
.

Shane. I don’t know why that is because it works fine for me even with a large folder with many subfolders. I ran my script on such a folder and got:

1 numbers
412 pdf
1 tax2018
2 txt

Your script returned:

412,pdf
2,txt
1,tax2018
1,numbers

Anyways, my script breaks with packages and is kludgey, so the OP should clearly use your script. I wish I had the skills to write stuff like that.

This version only returns extensions and omits anything from paths containing “/.”:

set theSourceFolder to (choose folder with prompt "Select an HDD or folder:")
set theSourceFolder to quoted form of POSIX path of theSourceFolder

set textFile to choose file name with prompt "Choose file name" default name "zCARPA.txt"
set textFile to quoted form of (POSIX path of textFile)

do shell script "find " & theSourceFolder & "  -type f | sed -En " & quoted form of "/.*\\/\\..*/ !s|.+\\.([^./]+$)|\\1|p" & " | sort | uniq -c > " & textFile

I had another look, and it appears the part-paths were appearing only for items in packages. That might be coincidence, but they’re also files without extensions.

There is, but it’s a bit tricky. You need to save it as a script library in ~/Library/Script Libraries, and call it from there. You would also have to convert theSourceFolder to an NSURL. So the code would be like:

use AppleScript version "2.3" -- macOS 10.9
use framework "Foundation"
use scripting additions

on reportOnFolder(theSourceFolder)
	set theSourceFolder to current application's |NSURL|'s fileURLWithPath:(POSIX path of theSourceFolder)
	-- get all files
	set fileManager to current application's NSFileManager's defaultManager()

       [...as above in here]

	close access fileRef
end reportOnFolder

You would need to save it as a .scptd bundle, and edit the bundle’s Info.plist file to include:

	<key>OSAAppleScriptObjCEnabled</key>
	<true/>

Then your main script would call it something like:

use AppleScript version "2.3"
use scripting additions
use theLib: script "Name of your lib file" -- change to suit

set theSourceFolder to (choose folder with prompt "Select an HDD or folder:")
tell theLib to reportOnFolder(theSourceFolder)

.
gentlemen,

thank you very much for all your solutions and kind help.

very much obliged to you.
.

Nigel. Your version works great. Just as a test, I ran the script on a huge folder on an external drive, and it’s surprising how fast it is (under a second).

For kicks, here’s a variation that pares down the sed (and won’t descend into packages):

do shell script "find -E " & theSourceFolder & " -iname '*.*' -prune ! -regex '.*/[.].*' | sed 's/.*[.]//g' | sort | uniq -c > " & textFile

FWIW, on my test folder (NAS drive, no packages) it comes up with lower numbers than Nigel’s script and my ASObjC version for several extensions. It also ( like mine) including folders.

For a shell script solution, I’d go for Marc’s rather than mine. It doesn’t descend into packages and it does actually include them! On the other hand, it doesn’t descend into folders with dots in their names either, presumably believing them to be packages.

“-iname” could perhaps be “-name”, since there’s no need for case insensitivity when looking for a dot. And "! -regex ‘./[.].’ " might be simplified to "! -path ‘/.’ " or possibly "! -name ‘.*’ ", either of which would also make the -E option unnecessary. But I doubt any of these make much difference in practice. The “s” command in the “sed” string doesn’t need the “g” because the regex used there initially includes everything in the line and then winds back looking for a dot, so the match is always to the last dot in the line anyway.