File pattern

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

File pattern

Post 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)
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: File pattern

Post 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)
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: File pattern

Post 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
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: File pattern

Post 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.
Have you ever noticed that software is never advertised using the adjective "spreadable".
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: File pattern

Post 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.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: File pattern

Post 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}"
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: File pattern

Post 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.
Cheers - Quin.
I code therefore I am
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: File pattern

Post 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.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: File pattern

Post by PJBlack »

thank you, however, the credit in this case goes to benoit, after all, i did nothing more than search the wiki ;-)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: File pattern

Post 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 .
If at first you don't succeed , try doing something differently.
BruceS
Post Reply