Page 1 of 2

File pattern

Posted: Saturday 10th June 2023 2:24pm
by cogier
Can someone tell me how to get this syntax correct, as the example below does not work. I have tried various things, but so far I have failed!

sDir = Dir(sPath, "*.jpg", "*.jpeg", "*.png", gb.File)

Re: File pattern

Posted: Saturday 10th June 2023 6:39pm
by vuott
As you know, unfortunately, the second parameter of the Dir() function is not an array; therefore, you cannot insert an array of type String either.
For now, I am only able to suggest this :? solution:
sDir = Dir(sPath, "*.*g", gb.File)

Re: File pattern

Posted: Saturday 10th June 2023 6:41pm
by PJBlack
complete description of dir you can find here:
https://gambaswiki.org/edit/lang/dir

this example did what you want i guess ...
' Print png and jpeg images in the user home directory.

DIM Directory AS String
DIM Files AS String[]
DIM FileName AS String

Directory = System.User.Home
Files = Dir(Directory, "*.png")
Files.Insert(Dir(Directory, "*.jpg"))
Files.Insert(Dir(Directory, "*.jpeg"))

FOR EACH FileName IN Files
  PRINT FileName
NEXT


hans seems to be a bit smarter than benoit ;-)
https://gambas-buch.de/doku.php?id=k6:k ... []=dir#dir

sDirectoryPath = User.Home &/ "BildTon"
' !!!!!!
sPattern = "[^0-9P]*.{png,jpg,gif}"

sFilter = gb.File  ' gb.Directory | gb.File + gb.Directory
bSorted = True
bMode = gb.Ascent  ' gb.Descent | gb.Ascent
If Dir(sDirectoryPath, sPattern, sFilter).Sort(bMode).Count = 0 Then
    Message.Info("The searched set is empty ...")
    Return
Else
   For Each sDirFileName In Dir(sDirectoryPath, sPattern, sFilter).Sort(bMode)
          Print sDirFileName
   Next
Endif

Re: File pattern

Posted: Saturday 10th June 2023 6:46pm
by thatbruce

Code: Select all

sdir = Dir(sPath, "{*.jpg,*.jpeg,*.png}", gb.File)
The pattern is a regexp expression as described in the help for LIKE.

Re: File pattern

Posted: Saturday 10th June 2023 6:58pm
by vuott
thatbruce wrote: Saturday 10th June 2023 6:46pm

Code: Select all

sdir = Dir(sPath, "{*.jpg,*.jpeg,*.png}", gb.File)
The pattern is a regexp expression as described in the help for LIKE.
...however, it seems :? to me that it doesn't work.

Re: File pattern

Posted: Saturday 10th June 2023 7:36pm
by vuott
PJBlack wrote: Saturday 10th June 2023 6:41pm sPattern = "[^0-9P]*.{png,jpg,gif}"
Very interesting !
I would also propose this string as Pattern:
"[^.]*.{png,jpg,gif}"

Re: File pattern

Posted: Saturday 10th June 2023 10:59pm
by Quincunxian
I use this setup in a 'toolbox' class that deals with files & folders.
Public SelectedFilter As String[]
Public FilterFile As String[] = ["*.*", "All Files"]
Public FilterImage As String[] = ["*.png;*.jpg;*.jpeg;*.bmp;*.webp;", "Picture files"]
Public FilterDocument As String[] = ["*.txt;*.odt", "Document files"]
Public FilterTextFiles As String[] = ["*.txt", "Text files"]
Public FilterMusic As String[] = ["*.ogg;*.mp3;*.wav;", "Audio files"]


I set the 'SelectedFilter to any of the variants prior to calling a file select form for whatever I'm looking for.

Re: File pattern

Posted: Sunday 11th June 2023 11:00am
by cogier
thatbruce. I tried exactly the same 'solution'. vuott said it didn't work for him, and, unfortunately, it didn't work for me either.
vuott. Your "*.*g" is a good workaround but is a bit hit-and-miss. I tried to use regexp, but I don't know the syntax well enough. I won't understand it if I look at it at a later date!
Quin. Your solution is close to what I have created to get around this issue.
PJBlack.
I like your .Insert method. I think this is the way I will go for now.

It would be handy if Pattern was an array.

Thanks guys for your input, appreciated.

Re: File pattern

Posted: Sunday 11th June 2023 12:19pm
by PJBlack
thank you, however, the credit in this case goes to benoit, after all, i did nothing more than search the wiki ;-)

Re: File pattern

Posted: Sunday 11th June 2023 12:32pm
by BruceSteers
How about just this....


  For Each s As String In Dir(User.Home, "*.{jpg,jpeg,png}", gb.file)
    Print s
  Next




Or the slightly more complicated looking version Vuott suggested.

  For Each s As String In Dir(User.Home, "[^.]*.{jpg,jpeg,png}", gb.file)
    Print s
  Next


Note: the [^.] prefix part just omits hidden files beginning with .