Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript count files in subfolders and label

Hello!


I have a set of folders that looks like this:


Main Folder

Sub Folder 1

Sub Sub Folder 1

Sub Sub Folder 2

Sub Folder 2

Sub Sub Folder 1

Sub Sub Folder 2

Sub Folder 3

Sub Sub Folder 1

Sub Sub Folder 2


I would like to count the number of files in each sub sub folder and label it "Sub Folder 1,Sub Sub Folder 1, 12" (12 being the count of files in Sub Sub Folder 1). So far I have this:


set theFolder to alias "Folderpath"

tell application "Finder"
	set downloadedShows to ""
	set k to 0
	repeat with subfolder2 in (get every folder of entire contents of folder theFolder)
		set k to k + 1
		set downloadedShows to downloadedShows & name of subfolder2 & ¬
			"," & (count files in subfolder2) & return
	end repeat
end tell


This returns:

"Sub Folder 1,count"

Sub Folder 2,count

Sub Folder 3,count

Sub Sub Folder 1,count

Sub Sub Folder 2,count" ..... etc


What I want it to return is:

"Sub Folder 1,Sub Sub Folder 1,count

Sub Folder 1,Sub Sub Folder 2,count

Sub Folder 2,Sub Sub Folder 1,count

Sub Folder 2,Sub Sub Folder 2,count" .....etc


Hoping someone can help me with this! Thank you!!

Mac OS X Server

Posted on May 22, 2021 2:48 PM

Reply
Question marked as Best reply

Posted on May 25, 2021 5:21 PM

The replacement code below will prompt you for the parent folder again, but this time, there will be no dialog with a list of subdir strings, but rather a text file written to your Desktop (subdirs.txt) containing the strings.


use scripting additions

set mainFolder to POSIX path of (choose folder default location (path to desktop)) as text
set OUTFILE to ((path to desktop as text) & "subdirs.txt") as text

set fileRef to (open for access OUTFILE with write permission)

set strList to paragraphs of my subfolders_with_count(mainFolder)

# now write (append) each text string to OUTFILE
repeat with astr in strList
	write (astr & return) to fileRef
end repeat
close access fileRef
(*
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
display dialog (items of strList) as text
set AppleScript's text item delimiters to TID
*)
return

on subfolders_with_count(afolder)
	return (do shell script "zsh -s <<'EOF' - " & afolder's quoted form & "

STARTFOLDER=\"${1}\"
for dirpath in \"${STARTFOLDER}\"*/*/;
do
	set -- \"${dirpath}\"/*
	printf '%s%d\\n' \"${dirpath:gs/\\//,/}\" \"$#\" | awk -F, '{printf \"%s,%s,%s\\n\", $6,$7,$8}'
done
EOF")
end subfolders_with_count


Similar questions

7 replies
Question marked as Best reply

May 25, 2021 5:21 PM in response to rachieeeroo

The replacement code below will prompt you for the parent folder again, but this time, there will be no dialog with a list of subdir strings, but rather a text file written to your Desktop (subdirs.txt) containing the strings.


use scripting additions

set mainFolder to POSIX path of (choose folder default location (path to desktop)) as text
set OUTFILE to ((path to desktop as text) & "subdirs.txt") as text

set fileRef to (open for access OUTFILE with write permission)

set strList to paragraphs of my subfolders_with_count(mainFolder)

# now write (append) each text string to OUTFILE
repeat with astr in strList
	write (astr & return) to fileRef
end repeat
close access fileRef
(*
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
display dialog (items of strList) as text
set AppleScript's text item delimiters to TID
*)
return

on subfolders_with_count(afolder)
	return (do shell script "zsh -s <<'EOF' - " & afolder's quoted form & "

STARTFOLDER=\"${1}\"
for dirpath in \"${STARTFOLDER}\"*/*/;
do
	set -- \"${dirpath}\"/*
	printf '%s%d\\n' \"${dirpath:gs/\\//,/}\" \"$#\" | awk -F, '{printf \"%s,%s,%s\\n\", $6,$7,$8}'
done
EOF")
end subfolders_with_count


Jun 12, 2021 10:24 AM in response to rachieeeroo

Same test folder hierarchy as before, and on local filesystem and mounted USB stick. Locations:

  1. /Users/username/Desktop/Metadata
  2. /Volumes/KINGSTON/Metadata


When the script is run on either choice above, it produces:


subdir1,dir1,3
subdir1,dir2,4
subdir1,dir3,5
subdir2,dir1,5
subdir2,dir2,3
subdir2,dir3,2
subdir3,dir1,1
subdir3,dir2,4
subdir3,dir3,5


use scripting additions

set mainFolder to POSIX path of (choose folder default location (path to desktop)) as text
set OUTFILE to ((path to desktop as text) & "subdirs.txt") as text

set fileRef to (open for access OUTFILE with write permission)

set strList to paragraphs of my subfolders_with_count(mainFolder)

# now write (append) each text string to OUTFILE
repeat with astr in strList
	write (astr & linefeed) to fileRef
end repeat
close access fileRef
return

on subfolders_with_count(afolder)
	return (do shell script "zsh -s <<'EOF' - " & afolder's quoted form & "

STARTFOLDER=\"${1}\"
for dirpath in \"${STARTFOLDER}\"*/*/;
do
	set -- \"${dirpath}\"/*
	if [[ ${STARTFOLDER} =~ \"Volume\" ]]; then
		# process USB mount point
		printf '%s%d\\n' \"${dirpath:gs/\\//,/}\" \"$#\" | awk -F, '{printf \"%s,%s,%s\\n\", $5,$6,$7}'
	else
		# local filesystem
		printf '%s%d\\n' \"${dirpath:gs/\\//,/}\" \"$#\" | awk -F, '{printf \"%s,%s,%s\\n\", $6,$7,$8}'
	fi
done
EOF")
end subfolders_with_count


macOS 11.4


May 23, 2021 9:17 AM in response to rachieeeroo

Given a directory hierarchy like the following:



produce the following output:



The following AppleScript using a Zsh shell handler will produce the above output when the main Metadata folder (in this example) is selected from the choose folder dialog of the script:


use scripting additions

set mainFolder to POSIX path of (choose folder default location (path to desktop)) as text

set strList to paragraphs of my subfolders_with_count(mainFolder)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
display dialog (items of strList) as text
set AppleScript's text item delimiters to TID
return

on subfolders_with_count(afolder)
	return (do shell script "zsh -s <<'EOF' - " & afolder's quoted form & "

STARTFOLDER=\"${1}\"
for dirpath in \"${STARTFOLDER}\"*/*/;
do
	set -- \"${dirpath}\"/*
	printf '%s%d\\n' \"${dirpath:gs/\\//,/}\" \"$#\" | awk -F, '{printf \"%s,%s,%s\\n\", $6,$7,$8}'
done
EOF")
end subfolders_with_count


Applescript count files in subfolders and label

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.