File pattern

Post

Posted
Rating:
#1 (In Topic #1054)
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
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!

Code (gambas)

  1. sDir = Dir(sPath, "*.jpg", "*.jpeg", "*.png", gb.File)
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
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:

Code (gambas)

  1. sDir = Dir(sPath, "*.*g", gb.File)

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
complete description of dir you can find here:
https://gambaswiki.org/edit/lang/dir

this example did what you want i guess …

Code (gambas)

  1. ' Print png and jpeg images in the user home directory.
  2.  
  3. DIM Directory AS String
  4. DIM Files AS String[]
  5. DIM FileName AS String
  6.  
  7. Directory = System.User.Home
  8. Files = Dir(Directory, "*.png")
  9. Files.Insert(Dir(Directory, "*.jpg"))
  10. Files.Insert(Dir(Directory, "*.jpeg"))
  11.  
  12. FOR EACH FileName IN Files
  13.   PRINT FileName

hans seems to be a bit smarter than benoit ;-)
https://gambas-buch.de…k6:k6.3:start&s[]=dir#dir

Code (gambas)

  1. sDirectoryPath = User.Home &/ "BildTon"
  2. ' !!!!!!
  3. sPattern = "[^0-9P]*.\{png,jpg,gif}"
  4.  
  5. sFilter = gb.File  ' gb.Directory | gb.File + gb.Directory
  6. bSorted = True
  7. bMode = gb.Ascent  ' gb.Descent | gb.Ascent
  8. If Dir(sDirectoryPath, sPattern, sFilter).Sort(bMode).Count = 0 Then
  9.     Message.Info("The searched set is empty ...")
  10.     Return
  11.    For Each sDirFileName In Dir(sDirectoryPath, sPattern, sFilter).Sort(bMode)
  12.           Print sDirFileName
  13.    Next
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

Code

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

Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
vuott is in the usergroup ‘Regular’

thatbruce said

Code

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 !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

PJBlack said

sPattern = "[^0-9P]*.{png,jpg,gif}"
Very interesting !
I would also propose this string as Pattern:

Code (gambas)

  1. "[^.]*.\{png,jpg,gif}"

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Quincunxian is in the usergroup ‘Blogger’
I use this setup in a 'toolbox' class that deals with files & folders.

Code (gambas)

  1. Public SelectedFilter As String[]
  2. Public FilterFile As String[] = ["*.*", "All Files"]
  3. Public FilterImage As String[] = ["*.png;*.jpg;*.jpeg;*.bmp;*.webp;", "Picture files"]
  4. Public FilterDocument As String[] = ["*.txt;*.odt", "Document files"]
  5. Public FilterTextFiles As String[] = ["*.txt", "Text files"]
  6. Public FilterMusic As String[] = ["*.ogg;*.mp3;*.wav;", "Audio files"]
  7.  

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
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
thank you, however, the credit in this case goes to benoit, after all, i did nothing more than search the wiki ;-)
Online now: No Back to the top

Post

Posted
Rating:
#10
Banned
How about just this….

Code (gambas)

  1.  
  2.   For Each s As String In Dir(User.Home, "*.\{jpg,jpeg,png}", gb.file)
  3.     Print s
  4.   Next
  5.  
  6.  


Or the slightly more complicated looking version Vuott suggested.

Code (gambas)

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

Note: the [^.] prefix part just omits hidden files beginning with .
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

cogier said

thatbruce. I tried exactly the same 'solution'. vuott said it didn't work for him, and, unfortunately, it didn't work for me either.

It would be handy if Pattern was an array.

Yeah, I gotta eat my own soup here. I grabbed it from an old project before checking that it actually was a valid regexp as expected by Dir(). vuott's expression (and BruceS' simplification) look much more promising.
best regards

Online now: No Back to the top
1 guest and 0 members have just viewed this.