Filter files in folder

Hi al

Playing with some script, thanks to the DJ but I am unable to add a option to say start with “Week”
Or Ends with or contains

This is the last line of the script that works OK to get al xlsm files in a folder
I believe I try anything but no luck

do shell script “find ‘/Users/mvp/Desktop/Print Test’ -iregex ‘./[^~][^/].xlsm,*’ -maxdepth 1” & streamEditorCommand without altering line endings

I hope anybody on the board can help me

I thought I have answered your questions :D. No problem btw here when a file begins with Week


do shell script "find $HOME/Desktop -iregex '.*/Week[^/]*\\.xls[bmx]*$' -maxdepth 1"

or when it ends with week :

do shell script "find $HOME/Desktop -iregex '.*/[^~][^/]*Week\\.xls[bmx]*$' -maxdepth 1"

Hi DJ

Great, That I understand now and is working in the VBA function but not what to do for contains

Can you explain how to do that

Another problem is that in my function I can choose if I want to filter on one or more extensions

When I use only xlsx, xlsb or xlsm I need [bmx] after the file extension to get the correct result

If I choose xls or [xlsx,xlsm] I must remove it to get the correct result

I now add a replace line to delete the bmx if one of the two options is selected.
Is that the correct way ?

Still looking for adding the contains option

I am a bit fed up with other stuff at the moment, so I think by this time tomorrow, I’ll have a finished handler for you, where you can have both a pattern and a list or a single extension.

I am glad I didin’t start it before now, because it is just now that I realize how it should work, that is, what you want such a handler to work!

Very often the solely most effective thing to when facing a problem, is to put it on ice for a while! :slight_smile:

Hi McUsr

We are close now to get a replacment for VBA Dir but it is not easy to get all options working

I alo notice now that I use two extensions that it also get the xls files, not good

Well. if you don’t want xls files, then you’d better drop the construct [mbs] on the end, and specify the file extension fully out.

Maybe if you change this


do shell script "find $HOME/Desktop -iregex '.*/Week[^/]*\\.xls[bmx]*$' -maxdepth 1"

to


do shell script "find $HOME/Desktop -iregex '.*/Week[^/]*\\.xls[bmx]+$' -maxdepth 1"

You can read about how this works the easy way by googling for regexp, or issue the command man grep in a terminal window.

Unfortunately there are several dialects with regular expressions, so one has to read the manual pages for the commands to make them work properly. :slight_smile:

Hi all

Almost there

Fixed the contains option now, there is only one problem left

When I use this line to get two extensions.[xlsx,xlsm] it also get all xls files
How can I fix this

do shell script “find ‘/Users/mvp/Desktop/Print Test’ -iregex ‘./[^~][^/]\.[xlsx,xlsm]*$’ -maxdepth 1” & streamEditorCommand without altering line endings

I think I did try to solve just that in the post above. If it doesn’t work, try to escap the ‘+’ with ‘\’ or add -E as an extra option to the find command, right after the directory path. :slight_smile:

I believe both lines you posted are the same but I go look again and try your suggestions.

If you look closely, you’ll see a ‘+’ at the end of the regexp at the lower line, instead of the ‘*’.

In regexp lingo ‘+’ means 1 or more, while ‘*’ means zero or more.

Oops, will try, I think tomorrow

1:11 here so bed time

Hi Ron,

I had a busy day/evening yesterday. But I think you’re looking for simething like this:

set streamEditorCommand to " | tr [/:] [:/] "
set streamEditorCommand to streamEditorCommand & " | sed -e " & quoted form of (" s.:." & (POSIX file "/" as string) & ".")
do shell script "find -E $HOME/Desktop -iregex '.*/([^~][^/]*Week[^/]*|Week[^/]*)\\.xls[bmx]*' -maxdepth 2" & streamEditorCommand

I’ve used a two expressions because the the [^~] takes away our first position of the name. So when a file name begins with ‘Week’ it won’t be listed. So with the option -E we’re able to do an OR operator and added a begins with expression as well. Too bad that find and grep don’t have an AND operator, I know only about perl has one. It would make the expression a bit easier.

Hi DJ

I think we hit the jackpot now.
Will do some more testing today and get back to you with the results

Thank you for sharing your brains

Hello, I am working on a general handler for you, not just for this case, so I wanted to know if you could give me some feedback about the specification below?

– Work in progress

to fileList(R)
	
end fileList
-- example call :
set xlFiles to fileList({sType:"file", sDepth:1, sPred:"regexp", sExt:{"xlsx", "xlsm", "xlsb"}, sTerm:"^Week.*$", spath:theFolder})

– Properties of R the parameter for filelist of type record:
– ====================================================

– sTerm: type string: MANDATORY

– The searchterm you are looking for, an empty search term will be interpreted as “" (all files).
– When it comes to specifying search terms the new type of regexp are allowed, so you can use
– terms like "expenses second quarter 200(4|5|6).
” or “expenses second quarter [[.201.]](0|1|2).*”
– See man grep under the -E option and much better man re_format(7).

– sPath: type string: MANDATORY
”””””””””””””””””””””””

– The searchterm is a posix path or hfs path (hierchial file sysem path) to a directory that must exist,
– it can end with “/” or “:” or not.

– sType: type string

– What to search for: “file”,“folder” or “both”. Optional, “file” is the default value.

– sDepth: type integer

– The depth of the search. Optional; defaults to 1.

– 1 specifies that the search is just to be performed in the given directory,
– just a positive number gives the number of levels to search, or the levels up that number.
– ( sed ‘d’ for større nivÃ¥er )
– (-1) is the full subtree below the given directory, all levels.

– sPred: type string

– The search predicate, specifies how the handler will interpret and use the searchTerm.
– Optional; defaults to “regexp”.

– If there are no extension given, then the searchterm will specify the full pattern to search
– for. Legal values:

– “starts with”:

” Matches files/folders that starts with the search term.

” “ends with”:

” Matches files/folders that ends with the search term, it is not smart in the sense that it will not match up to
” an extension, if none are given, if this is what you want, then you’d specify an sExt of “”. This is not default, as
– some like the mother, other prefers the daughter!

” Example:
– if your specify ends with, and the filename is hackerne.ws and you search for “ws”, without an extension, then ”
” you will miss this file.

– “Contains”:
” Matches files/folders that contains the searchterm somewhere (see Ends with.)

– “Exact”:
” Exact match.

” “Regexp”:
” The whole regexp is solely provided by the user, either with or without sExt, but considering it if it is there. if
” you want all files, then don’t specify the sExt, set sPred to “regexp” and sTerm to “” or “*”.

– sExt: type list

– The search extension, specifies which extension the file/folder list will be filtered against.
– It works in conjunction with sTerm, it takes a list of file extensions, spelled fully out.
– Example: {“xls”,“xlsm”,“xlsx”,“xlsb”} is a valid list of extensions{“xls[bmx]”} is discouraged
– but it works.
– Optional parameter, default value {“*”} which means that any extension is allowed, but you must
– specify and extension of {“”} to make sure that nothing will be interpreted as extensions though.

– sVisible: type boolean

– If invisible files are to be included or not. Optional, defaults to not include invisible files.

– Returns a file list:
– ====================

– The file list will be returned in the format the search directory was given, posix path, or hfs.

Hi McUsr,

You should take a look at the site of Ron and you know that he needs to convert this AS code into VBA code executing AS Code with MacScript command. This means that giving him an all-solution handler doesn’t really help him much because the pieces of AS code is only used in the complete VBA Code. Ron needs to create dynamically AS code depending on given input as you can find here. Just saying to save you time and energy

Hi McUsr

Like DJ already told you I am using VBA code to build and call the script

It is a one liner that call the function with the applescript

Call GetFilesOnMacWithOrWithoutSubfolders(Level:=1, ExtChoice:=0, FileFilterOption:=0, FileNameFilterStr:=“SearchString”)
'Level : 1= Only the files in the folder, 2 to ? levels of subfolders
'ExtChoice : 0=Start with xls, 1=xls , 2=xlsx, 3=xlsm, 4=xlsb, 5=csv, 6=txt, 7=all files, 8=(xlsx,xlsm,xlsb), 9=(csv,txt)
'FileFilterOption : 0=No Filter, 1=Begins, 2=Ends, 3=Contains
'FileNameFilterStr : Search string used when FileFilterOption = 1, 2 or 3

I will add it to my site with a workbook that you can download and test so you know how I use it
Will post the link this evening so you can try

Thank you very much

@ DJ
Maybe i should have checked out his site a little bit better, the handler is meant to be generally useful, so I’ll make it anyway I better go take a nap, to get all that energy! :smiley:

@ Ron
I think I better leave you and DJ in peace with your efforts. (I reserve the right to snag whatever DJ produces that I may find useful. :slight_smile: )

Here is a link
http://www.rondebruin.nl/mac.htm#Loop

In the Workbook you can see it if you want

Thank you very much both of you to be helping in this forum.