wildcard

Hi all

playing with some script to get the count of files in a folder
For example :

whose name extension is “txt”

Because I call this script with VBA the extension string is a variable and I call a VBA function like this

                NumFiles = CountFilesInFolder(TextBoxDirectory.Text, "{""xls"", ""xlsx"", ""xlsm""}")

working OK

What can I do if I want to count all files in the folder without adding them all in the code, can I use a wildcard or so like this ?

whose name extension is “*”

Thanks for reading

Hi,

wildcards in AppleScript can be realized with


whose name extension is in {"xls", "xlsx", "xlsm"}
whose name extension starts with "xl"
whose name extension ends with "sm"
whose name extension contains "ls"
-- combined forms are possible too
whose name extension starts with "xl" and name extension ends with "sm"

Cool

Is there also something like *

In Windows we can use .* or . for every file without knowing the type of files that are in the folder

the AppleScript equivalent is

.* = every file of folder xyz whose name extension is not missing value and name extension is not ""

*.*  = every file of folder xyz

Hi Stefan

Thanks for your help

Have a great day

I found that the actual wildcard itself is not possible if used as for example “*.jpg” or “img.*

Hi all

Add a example on my site so we can use it in VBA
http://www.rondebruin.nl/mac.htm#Count

Hello. I am sure you can derive a counting handler from this. :wink:


set thePattern to "*"
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to flist for thePattern against thePxFolder
length of thefiles

to flist for apattern against apxfolder
	try
		return paragraphs of (do shell script "cd " & apxfolder & "; ls  " & apattern)
	on error
		return null
	end try
end flist

Edit

The unix underpinnings used here as power beyond belief in comparision to what you windows guys are used to.
the “.” is really the wildcard for one character, the ls, is the equivalent of dir, but go to the terminal window and see what it can do by issuing “man ls”.

The correct pattern for .xls woud really be ".xls".

You should both go through a unix tutorial for beginners.

Hi (again) Ron,

The Finder is quite slow in finding files. I’d suggest you use System Events or do shell script. But filtering on multiple file extensions is not possible with system events (at least on 10.6.8). So you need multiple lines:

tell application "System Events"
	set theFiles to {}
	set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xls")
	set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xlsx")
	set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xlsm")
end tell
return theFiles

The do shell script method you can use grep and using multiple expressions so you can keep your code in one line.

every paragraph of (do shell script "ls $HOME/Desktop | egrep  '*.xls$|*.xlsx$|*.xlsm$'")

Or you can even do the counting in the shell:

(do shell script "ls $HOME/Desktop | egrep  '*.xls$|*.xlsx$|*.xlsm$' | wc -l") as integer

Pick one you like most :smiley:

Edit: Here a small handler that creates the expressesion for egrep.
Edit2: as Nigel suggested it’s better to look for the extensions name beginning with an period rather than looking of the name ends with.

set expr to expressionForMultipleFileExtensions({"scpt", "xls"})
do shell script "ls $HOME/Desktop | egrep " & quoted form of expr & " &2>/dev/null"

on expressionForMultipleFileExtensions(e)
	set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|\\."}
	set expr to ("\\." & e as string) & "$"
	set AppleScript's text item delimiters to oldTID
	return expr
end expressionForMultipleFileExtensions

:slight_smile:

tell application "System Events" to set theFiles to (name of every disk item of desktop folder whose name extension is "xls" or name extension is "xlsx" or name extension is "xlsm")
every paragraph of (do shell script "ls $HOME/Desktop | egrep  '\\.xls[xm]?$' &2>/dev/null")

Edit: Error redirection added to shell script for when there are no matches.
Edit: Dot in the regex escaped to ensure the literal character!

:facepalm: :smiley:

At least we agree upon ls for do shell script.

Mdfind gives too much, and find is to slow.

I like DJ Bazzie Wazzie’s handler for making the expression, as it becomes more usable, in a handler for that purpose.

I nicked it, and will use it for something in codexchange in the thread with the mdfind (folders) handler.


set thePattern to {"html", "txt"}
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to flistByExt for thePattern against thePxFolder
length of thefiles

to flistByExt for apattern against apxfolder
” Building of RegExp pattern by DJ Bazzie Wazzie http://macscripter.net/edit.php?id=157205
	local oldTID, expr
	set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|*."}
	set expr to quoted form of ("*." & apattern as string) & "$"
	set AppleScript's text item delimiters to oldTID
	try
		return paragraphs of (do shell script "ls " & apxfolder & " |  egrep " & expr)
	on error
		return null
	end try
end flistByExt

The egrep script needs to catch the error when there’s no match. I’ve amended it (edit: and now the regex) in my post above.

Thanks, I’ve changed it as Nigel suggested. The expression will now be ends with “.xxx” instead of ends with “xxx”.

Edit: But now the handlers doesn’t work when the extension list is {“”} :frowning:

Hello!

The handler I posted in code exchange looks like this, and it worked in all cases! :wink:


set thePattern to {"html", "txt"}
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to listFilesByExt for thePxFolder by thePattern
length of thefiles

to listFilesByExt for aPosixFolder by extensionsList
	-- Building of RegExp pattern by DJ Bazzie Wazzie http://macscripter.net/edit.php?id=157205
	local oldTID, expr
	set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|*."}
	set expr to quoted form of ("*." & extensionsList as string) & "$"
	set AppleScript's text item delimiters to oldTID
	try
		return paragraphs of (do shell script "ls " & aPosixFolder & " |  egrep " & expr)
	on error
		return null
	end try
end listFilesByExt


As long as we know we are dealing with file extensions here, there is no practical importance to whether the dot is a literal dot or a regexp dot. if the user wants literal dots, the user can specify “\.html” as an extension.

Normally this shouldn’t be any problem.

Hello!

I was to hasty, if you search by extension, then the extension should be easy to use, like it is with your solution DJ Bazzie Wazzie, even though it doesn’t return anything when not given any arguments.

I’ll fix it later on, and toss in the “original” handler, for getting a filelist by one searchpattern? :slight_smile: That will give you a full listing (without directories?) of a folder.

I have to ponder that too, having pondered through the fileListByExt, where I landed on your and Nigel’s approach.

It is too fast today, and I am out! :slight_smile:

Do you have any suggestions for a filelist by a pattern (or more), in a folder, feel free!

set expr to expressionForMultipleFileExtensions({"scpt", "", "txt"})
do shell script "ls $HOME/Desktop | egrep " & quoted form of expr & " &2>/dev/null"

on expressionForMultipleFileExtensions(e)
	set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "|"}
	set expr to ("(|" & e & "|)")
	set AppleScript's text item delimiters to oldTID
	
	return (do shell script ("sed 's/||\\(.*\\)/|\\1|^[^.]+/ ; s/(|)|// ; s/(|/.(/ ; s/|)/)/ ; s/^/(/ ; s/$/)$/' <<<" & quoted form of expr))
end expressionForMultipleFileExtensions

This board is becoming MacShellScripter more and more :confused:

Well, you have contributed to it as well.:wink: Well, here all solutions are served. And someone found Nietzsche’s hammer! :smiley:

But seriously, often the right solution to a problem around here, is a shell script. Take wildcard for instance. Why not adapt a little bit, to make it easier for scripters, than having the scripters adapt, as far as I remember, there were something to pick up with AppleScript.

Applescript is also a glue language, I think it to be a little bit in the spirit of it, to grab whatever you can and use it. It is object-orientation. Options are also good to have.

@ Nigel:
I thought the problem through earlier, if you make a handler that search for file-extensions, why should the handler then work, when no extension are deliberately given, wouldn’t it then, philosophically be better to use another general handler than the specialist one? :slight_smile:

I’d rather throw an error message, if no extension are specified for that particular handler, but that is me.

I understand that, but sometimes it looks like to take a sledgehammer to crack a nut

The OP just asked for the AppleScript equivalent for a given VBA function to count files in one folder