Copy File Names to Clipboard

I’ve been struggling with this script for a couple days now. I just can’t seem to wrestle it into submission!

I want to get the names of all the files in a folder, sorted by subfolder. I want those names in the clipboard so I can paste it into an email. So far I’m trying to write everything to a txt file and then copy the txt file text and delete the txt file. But those lines are giving me errors. Of course, if I can write straight to the clipboard somehow, those errors are moot.

Secondarily, I want the parent folder not to appear in the list. currently is exports this:

and I want it to export this

A minor difference, I know. But saving every second counts.

Any thoughts?


property file_id : null

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to ((path to desktop as string) & "Folder Inventory.txt")
set file_id to (open for access file log_file_path with write permission)
my do_subfolders(this_folder, "")
write (return & return) to file_id starting at eof
set the clipboard to (read file log_file_path) --Gives error--
delete file log_file_path --Gives error--
close access file_id
say "finished"
return

on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		write (return & return & name_prefix & (name of this_folder) as string) to file_id starting at eof
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & " " & (name of this_file) as string
			write (return & name_prefix & " " & (name of this_file) as string) to file_id starting at eof
		end repeat
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix)
		end repeat
	end tell
end do_subfolders

You may try :


property file_id : null

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to ((path to desktop as string) & "Folder Inventory.txt")
set file_id to (open for access file log_file_path with write permission)
my do_subfolders(this_folder, "")
write (return & return) to file_id starting at eof
set the clipboard to (read file log_file_path) --Gives error--
delete file log_file_path --Gives error--
close access file_id
say "finished"
return

on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		if name_prefix is not "" then write (return & return & name_prefix & (name of this_folder) as string) to file_id starting at eof # EDITED
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & " " & (name of this_file) as string
			write (return & name_prefix & " " & (name of this_file) as string) to file_id starting at eof
		end repeat
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix)
		end repeat
	end tell
end do_subfolders

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) jeudi 23 juillet 2015 19:38:44

Thanks a bunch, Yvan! That solves the issue of the parent folder in the list.

Now I just need to solve the clipboard issue.

Got it working.
Apparently I needed to tell Finder to copy and delete the file.
It’s always the simple stuff…


property file_id : null

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to ((path to desktop as string) & "Folder Inventory.txt")
set file_id to (open for access file log_file_path with write permission)
my do_subfolders(this_folder, "")
close access file_id
set the clipboard to (read file log_file_path)
tell application "Finder" to delete log_file_path
say "finished"
return

on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		if name_prefix is not "" then write (name_prefix & (name of this_folder) & return as string) to file_id starting at eof
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & " " & (name of this_file) as string
			write (name_prefix & " " & (name of this_file) & return as string) to file_id starting at eof
		end repeat
		write (return as string) to file_id starting at eof
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix)
		end repeat
	end tell
end do_subfolders

set this_folder to choose folder with prompt "Choose the root folder"
set theListing to my do_subfolders(this_folder, "", "")
set the clipboard to theListing

on do_subfolders(this_folder, name_prefix, theListing)
	tell application "Finder"
		if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of this_folder) & return as string)
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & " " & (name of this_file) as string
			set theListing to theListing & (name_prefix & " " & (name of this_file) & return as string)
		end repeat
		set theListing to theListing & (return as string)
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, " " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders

I’ve been using a slightly modified form of the script above with great success, but now I’d like to expand it a bit and wonder if I could ask for some additions.

I’m using it to extract specific file types from any sub level of the chosen folder, and that works fine as far as listing the desired files in their containing folders, except that it also includes folders that don’t have any of the desired file types, so I’d like to suppress these irrelevant folders.

If it helps any, these “empty” folders all have a single blank line under them in theListing. I’ve been trying to eliminate them by doing a second pass on theListing and working on that blank line, but I’m not getting it.

Then, along with choosing a folder to search, I’d also like to give the user a choice of any number of file extensions to include on the list. Default: “avi mp4 mov mkv html flv” with single space delimiter. Blank means Any.

The above is looking just for file extensions, and that would be my primary focus, but I think it would be even more useful if the user could enter any number of arbitrary strings to search for embedded in every file name in the chosen folder. Is that too ambitious?

Finally (honest!), I’d like a switch included in the Dialogbox to include (or not) invisibles in theListing.

Here’s what I’ve got that’s working well, if slowly. It looked at ~500k files in my Applications folder and found the single .avi file that was buried 10 folders deep. But it also included ~4k folders that didn’t have .avi files.

I see that applying the Applescript code tags has fiddled with the spacing of my code. Where I have
“(name_prefix & " " & (name of this_file)”, the evil tagger has changed it to “(name_prefix & " " & (name of this_file)”
That’s supposed to be 5 spaces between the quote marks. This affects the hierarchical indenting of theList, and probably bit the original uploader of this code. There’s 2 other places in the code where this applies.


(*
http://macscripter.net/viewtopic.php?id=44118

Search in all subfolder levels of the chosen folder for files with extension: extn
Output to clipboard as list, including folder hierarchy

Very SLOW with large folders. 
*)

property extn : "avi"  --include only files with this extension

set this_folder to choose folder with prompt "Choose a folder to search for files with the extension: " & extn
set theListing to my do_subfolders(this_folder, "", "")
 
set the clipboard to theListing

on do_subfolders(this_folder, name_prefix, theListing)
	tell application "Finder"
		if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of this_folder) & return as string)
		set file_list to every file in this_folder whose name extension is extn
		repeat with this_file in file_list
			log name_prefix & "    " & (name of this_file) as string
			set theListing to theListing & (name_prefix & "    " & (name of this_file) & return as string)
		end repeat
		set theListing to theListing & (return as string)
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders

I guess that if you replace :
tell application “Finder”
by
tell application “System Events”

the script will be faster.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) lundi 30 novembre 2015 12:02:01

This approach is designed for where the hierarchy is not very deep, and will always be slow when searching a big hierarchy.

You should be able to eliminate the folders with none of the requisite files by swapping the order of a couple of lines, so that when you get file_list, check its count, and only add the filename (what was the previous line) if there’s more than 1.

You can’t (easily) add a checkbox to the dialog. You can use either a choice of extensions or some strings; you just need to change the whose clause (eg, …every file in this_folder whose name extension is in listOfExtensions).

If I understand correctly your question, this edited version get rid of folders with no matching file.


(*
http://macscripter.net/viewtopic.php?id=44118

Search in all subfolder levels of the chosen folder for files with extension: extn
Output to clipboard as list, including folder hierarchy

Very SLOW with large folders. 
*)

property extn : "png" --"avi" --include only files with this extension

set this_folder to choose folder with prompt "Choose a folder to search for files with the extension: " & extn
set beg to current date
set theListing to my do_subfolders(this_folder, "", "")
set the clipboard to theListing
display dialog "Done in : " & ((current date) - beg) & " seconds"
--> "Done in : 7 seconds"
on do_subfolders(this_folder, name_prefix, theListing)
	tell application "System Events"
		set file_list to every file in this_folder whose name extension is extn
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of this_folder) & return as string)
			repeat with this_file in file_list
				log name_prefix & "    " & (name of this_file) as string
				set theListing to theListing & (name_prefix & "    " & (name of this_file) & return as string)
			end repeat
			set theListing to theListing &return
		end if
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders

If you read carefully you will see that I replaced avi by png.
You will also see that I removed several unneeded coercions to string.
Applying it to my Applications folder, it required 7 seconds to do the job.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) lundi 30 novembre 2015 14:28:44

Thanks guys, that works great. Just the kind of hierarchical listing I was looking for. And much faster. On a 83 GB Apps Folder it took 32 seconds, and on a different machine with 17 GB Apps Folder it took 2 seconds. Very acceptable for my purposes.

Now, in my attempt to make it more universal, here’s how I tried to get a user selection of file extensions to be included in the list of files. Evidently I’m not doing the text-to-list conversion correctly.

I’m leaving the timer dialog box in because it’s useful, and in that same box I’d also like to add a count of the number of file names returned.

Here’s what I have (not working).


(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search in all subfolder levels of the chosen folder for files with any extension on the list
Output to clipboard as list, including folder hierarchy

*)

property fileTypes : {}
set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the listing for the chosen folder." default answer "avi mp4 mkv flv mov html") as text


set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set fileTypes to fileTypes as list
set AppleScript's text item delimiters to tids
-- return fileTypes = {"avi mp4 mkv flv mov html"}


set theFolder to choose folder with prompt "Choose folder to list its files with chosen file extensions"
set beg to current date

set theListing to my do_subfolders(theFolder, "", "")
set the clipboard to theListing
display dialog "Done in : " & ((current date) - beg) & " seconds"

on do_subfolders(theFolder, name_prefix, theListing)
	tell application "System Events"
		set file_list to every file in theFolder whose name extension is in fileTypes
		-- error "System Events got an error: Can't make {\"avi mp4 mkv flv mov html\"} into type specifier." number -1700 from {"avi mp4 mkv flv mov html"} to specifier
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of theFolder) & return as string)
			repeat with this_file in file_list
				log name_prefix & "     " & (name of this_file) as string
				set theListing to theListing & (name_prefix & "     " & (name of this_file) & return as string)
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders

The returned error message is clear and explicit (not always the case) which states :
Résultat :
error “System Events got an error: Can’t make {"avi mp4 mkv flv mov html"} into type specifier.” number -1700 from {“avi mp4 mkv flv mov html”} to specifier
If you look carefully, you may see that you aren’t passing a list of extensions but a list containing the string “avi mp4 mkv flv mov html”
You didn’t use the correct syntax to split the string into its components.
The correct syntax is :
set fileTypes to text items of fileTypes # WAS WRONGLY CODED

Running the edited script issue the error :
error “Erreur dans System Events : Impossible de convertir {"avi", "mp4", "mkv", "flv", "mov", "html"} en type specifier.” number -1700 from {“avi”, “mp4”, “mkv”, “flv”, “mov”, “html”} to specifier

It’s because you can’t apply whose in such case. You must use a loop like :
set filesAvailable to every file in theFolder
set file_list to {}
# Use a loop
repeat with afile in filesAvailable
try
if (name extension of afile) is in fileTypes then set end of file_list to contents of afile
end try
end repeat
The try is required because extracting the name extension of a folder issue an error:
error “Erreur dans System Events : Il est impossible d’obtenir file "Macintosh HD:Users:Important:archivage Kbase".” number -1728 from file “Macintosh HD:Users:Important:archivage Kbase”
This time it’s a bit odd because it’s System Events which inserted a reference to a folder when we asked it to return the list of files available in the scanned folder.

Run this edited version:


(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search in all subfolder levels of the chosen folder for files with any extension on the list
Output to clipboard as list, including folder hierarchy

*)

property fileTypes : {}
set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the listing for the chosen folder." default answer "avi mp4 mkv flv mov html") as text


set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set fileTypes to text items of fileTypes # WAS WRONGLY CODED
set AppleScript's text item delimiters to tids
-- return fileTypes = {"avi mp4 mkv flv mov html"}


set theFolder to choose folder with prompt "Choose folder to list its files with chosen file extensions"
set beg to current date

set theListing to my do_subfolders(theFolder, "", "")
set the clipboard to theListing
display dialog "Done in : " & ((current date) - beg) & " seconds"

on do_subfolders(theFolder, name_prefix, theListing)
	log class of fileTypes
	tell application "System Events"
		set filesAvailable to every file in theFolder
		set file_list to {}
		# Use a loop
		repeat with afile in filesAvailable
			try
				if (name extension of afile) is in fileTypes then set end of file_list to contents of afile
			end try
		end repeat
		-- error "System Events got an error: Can't make {\"avi mp4 mkv flv mov html\"} into type specifier." number -1700 from {"avi mp4 mkv flv mov html"} to specifier
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of theFolder) & return as string)
			repeat with this_file in file_list
				log name_prefix & "     " & (name of this_file) as string
				set theListing to theListing & (name_prefix & "     " & (name of this_file) & return as string)
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders

Here, it scanned my Applications folder in 21 seconds

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) mercredi 2 décembre 2015 12:39:12

That works perfectly Yvan, thank-you very much.

I’ve added the option to leave the extensions list blank to indicate that all files in the folder should be listed, and I put a count of returned files in the timer dialog box.

An unexpected bonus is the fact that the display dialog box will ignore extra spaces in the list of desired extensions. e.g. this still works properly if the user enters " avi mp4 "

I’ve stripped out my bad code and all the error messages, and this is the finished result for those looking for such a thing. As mentioned above, the code tagger on this site is still changing the number of spaces in the code, so to make the printout look a little better, these spaces can be adjusted from what’s shown below.

(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search all subfolder levels of the chosen folder and list any files whose extension is on the list
Output to clipboard as list, including folder hierarchy
*)

property fileTypes : {}
property listAllFlag : false
property noneFound : " No files were found with the entered extensions"
property fileCount : 0

set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the file list for the chosen folder. Leave empty to list all files." default answer "flv avi mp4 mkv mov html") as text

set listAllFlag to ((count fileTypes) = 0) -- if no extensions entered then list all files

if listAllFlag then
	set theFolder to choose folder with prompt "Choose folder to list all its files"
else
	set theFolder to choose folder with prompt "Choose folder to list its files with the entered file extensions"
end if

set beg to current date
set fileCount to 0
set theListing to my do_subfolders(theFolder, "", "")

if fileCount > 0 then
	set the clipboard to theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & fileCount & " files were found and left on the Clipboard"
else
	set the clipboard to noneFound --else the Clipboard still retains old data which could look like a newly returned theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & noneFound
end if

on do_subfolders(theFolder, name_prefix, theListing)
	log class of fileTypes
	tell application "System Events"
		set filesAvailable to every file in theFolder
		set file_list to {}
		
		repeat with afile in filesAvailable
			try
				if listAllFlag then --no file extensions were entered so list all files
					set end of file_list to contents of afile
					set fileCount to fileCount + 1
				else --some file extensions were entered so include this file if its extension is on the list
					if (name extension of afile) is in fileTypes then
						set end of file_list to contents of afile
						set fileCount to fileCount + 1
					end if
				end if
			end try
		end repeat
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & (name_prefix & (name of theFolder) & return as string)
			repeat with this_file in file_list
				log name_prefix & "     " & (name of this_file) as string
				set theListing to theListing & (name_prefix & "     " & (name of this_file) & return as string)
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
		return theListing
	end tell
end do_subfolders
return fileCount

I made a bit of complementary cleaning.

(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search all subfolder levels of the chosen folder and list any files whose extension is on the list
Output to clipboard as list, including folder hierarchy
*)

property fileTypes : {}
property listAllFlag : false
property fileCount : 0


set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the file list for the chosen folder. Leave empty to list all files." default answer "flv avi mp4 mkv mov html") --as text # No need to coerce as text, text returned IS a text object

-- Here, fileTypes is the string "avi mp4 mkv flv mov html" # ADDED

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space} # ADDED
set fileTypes to text items of fileTypes # WAS WRONGLY CODED # ADDED
set AppleScript's text item delimiters to tids # ADDED

-- Here, fileTypes is the list {"flv", "avi", "mp4", "mkv", "mov", "html"}

--set listAllFlag to ((count fileTypes) = 0) -- if no extensions entered then list all files
set listAllFlag to fileTypes = "" -- if no extensions entered then list all files

if listAllFlag then
	set theFolder to choose folder with prompt "Choose folder to list all its files"
else
	set theFolder to choose folder with prompt "Choose folder to list its files with the entered file extensions"
end if

set beg to current date
set fileCount to 0
set theListing to my do_subfolders(theFolder, "", "")

if fileCount > 0 then
	set the clipboard to theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & fileCount & " files were found and left on the Clipboard"
else
	set noneFound to " No files were found with the entered extensions" # No need to define it as a property
	set the clipboard to noneFound --else the Clipboard still retains old data which could look like a newly returned theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & noneFound
end if

return fileCount # What need for that ?

# No more main code after this comment, only handler(s) are allowed!

on do_subfolders(theFolder, name_prefix, theListing) # Here, name_prefix and theListing ARE strings
	--log class of fileTypes
	tell application "System Events"
		set filesAvailable to every file in theFolder
		set file_list to {}
		
		repeat with afile in filesAvailable
			try
				if listAllFlag then --no file extensions were entered so list all files
					set end of file_list to contents of afile
					set fileCount to fileCount + 1
				else --some file extensions were entered so include this file if its extension is on the list
					if (name extension of afile) is in fileTypes then
						set end of file_list to contents of afile
						set fileCount to fileCount + 1
					end if
				end if
			end try
		end repeat
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & name_prefix & (name of theFolder) & return
			repeat with this_file in file_list
				--log name_prefix & "     " & (name of this_file)
				set theListing to theListing & name_prefix & "     " & (name of this_file) & return
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
	end tell
	return theListing # May be better to return AFTER telling explicitely to System Events that we no longer speak to it.
end do_subfolders

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) jeudi 3 décembre 2015 11:11:50

Thanks for following up Yvan. Much appreciated.

It seems I still have a problem with this script. When I enter an extension to search for, it finds all files with that extension, but it also finds files whose extensions start with the same letter as the desired extension and which have the same number or fewer letters. For instance, here’s a list of extensions entered in the dialog box (one at a time), and the file types returned

extn
entered – finds files with these extns

html .h
.htm
.html

htm .h
.htm

ht .h

h .h

htmlabc .h
.htm
.html

mov .m
.mov

I’ll include the script again with all of Yvan’s improvements.


(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search all subfolder levels of the chosen folder and return all files whose extension is on the user list
Output to clipboard as list, including folder hierarchy
*)

property fileTypes : {}
property listAllFlag : false
property fileCount : 0


set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the file list for the chosen folder. Leave empty to list all files." default answer "flv avi mp4 mkv mov html")

set listAllFlag to fileTypes = "" -- if no extensions entered then list all files

if listAllFlag then
	set theFolder to choose folder with prompt "Choose folder to list all its files"
else
	set theFolder to choose folder with prompt "Choose folder to list its files with the entered file extensions"
end if

set beg to current date
set fileCount to 0
set theListing to my do_subfolders(theFolder, "", "")

if fileCount > 0 then
	set the clipboard to theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & fileCount & " files were found and left on the Clipboard"
else
	set noneFound to " No files were found with the entered extensions"
	set the clipboard to noneFound --else the Clipboard still retains old data which could look like a newly returned theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & noneFound
end if

on do_subfolders(theFolder, name_prefix, theListing)
	tell application "System Events"
		set filesAvailable to every file in theFolder
		set file_list to {}
		
		repeat with afile in filesAvailable
			try
				if listAllFlag then --no file extensions were entered so list all files
					set end of file_list to contents of afile
					set fileCount to fileCount + 1
				else --at least one file extension was entered so include this file if its extension is on the list
					if (name extension of afile) is in fileTypes then
						set end of file_list to contents of afile
						set fileCount to fileCount + 1
					end if
				end if
			end try
		end repeat
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & name_prefix & (name of theFolder) & return
			repeat with this_file in file_list
				set theListing to theListing & name_prefix & "     " & (name of this_file) & return
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
	end tell
	return theListing # May be better to return AFTER telling System Events explicitly that we no longer speak to it.
end do_subfolders

When I “made a bit of complementary cleaning”, I missed the fact that you dropped a group of three instructions which I added in a preceding version.

They are :

set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, space}
set fileTypes to text items of fileTypes # WAS WRONGLY CODED
set AppleScript’s text item delimiters to tids

Without them your code check that the found extension is available in the STRING “flv avi mp4 mkv mov html”
This is why it accepts htm, h and would accept fl,f, av, a,mp, m, mk, ht, h.

With the three instructions the script no longer check that the found extension is available in the string but it search that it’s available in the list {“flv”, “avi”, “mp4”, “mkv”, “mov”, “html”}

So, I’m sure that you will get better results with :


(*
http://macscripter.net/viewtopic.php?id=44118

Get user list of file extensions to search for in Choose Folder
Search all subfolder levels of the chosen folder and return all files whose extension is on the user list
Output to clipboard as list, including folder hierarchy
*)

property fileTypes : {}
property listAllFlag : false
property fileCount : 0


set fileTypes to text returned of (display dialog "Enter any number of file extensions, separated by a single space, to be included on the file list for the chosen folder. Leave empty to list all files." default answer "flv avi mp4 mkv mov html")

-- Here, fileTypes is the string "avi mp4 mkv flv mov html"

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set fileTypes to text items of fileTypes # WAS WRONGLY CODED
set AppleScript's text item delimiters to tids

-- Here, fileTypes is the list {"flv", "avi", "mp4", "mkv", "mov", "html"}

set listAllFlag to fileTypes = "" -- if no extensions entered then list all files

if listAllFlag then
	set theFolder to choose folder with prompt "Choose folder to list all its files"
else
	set theFolder to choose folder with prompt "Choose folder to list its files with the entered file extensions"
end if

set beg to current date
set fileCount to 0
set theListing to my do_subfolders(theFolder, "", "")

if fileCount > 0 then
	set the clipboard to theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & fileCount & " files were found and left on the Clipboard"
else
	set noneFound to " No files were found with the entered extensions"
	set the clipboard to noneFound --else the Clipboard still retains old data which could look like a newly returned theListing
	display dialog "Done in : " & ((current date) - beg) & " seconds" & return & noneFound
end if

on do_subfolders(theFolder, name_prefix, theListing)
	tell application "System Events"
		set filesAvailable to every file in theFolder
		set file_list to {}
		
		repeat with afile in filesAvailable
			try
				if listAllFlag then --no file extensions were entered so list all files
					set end of file_list to contents of afile
					set fileCount to fileCount + 1
				else --at least one file extension was entered so include this file if its extension is on the list
					if (name extension of afile) is in fileTypes then
						set end of file_list to contents of afile
						set fileCount to fileCount + 1
					end if
				end if
			end try
		end repeat
		
		if file_list is not {} then
			if name_prefix is not "" then set theListing to theListing & name_prefix & (name of theFolder) & return
			repeat with this_file in file_list
				set theListing to theListing & name_prefix & "     " & (name of this_file) & return
			end repeat
			set theListing to theListing & return
		end if
		set folder_list to every folder in theFolder
		repeat with this_subfolder in folder_list
			set theListing to my do_subfolders(this_subfolder, "     " & name_prefix, theListing)
		end repeat
	end tell
	return theListing # May be better to return AFTER telling System Events explicitly that we no longer speak to it.
end do_subfolders

For safe, I edit also my preceding message.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) vendredi 4 décembre 2015 17:03:13

That fixed it. Thanks again Yvan. Sorry I made more work for you.

Don’t worry.
It’s my fault if I posted a wrong version.
At 11:11 French time, I was supposed to be fully awake and not already sufficiently tired to miss that.

Yvan KOENIG (VALLAURIS, France) vendredi 4 décembre 2015 23:23:24