Multiple file extensions in ExamineDirectory() ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Multiple file extensions in ExamineDirectory() ?

Post by kenmo »

Hello guys,

I want to search through various folders for files of certain (multiple) extensions. Such as audio files: mp3, wav, m4a, etc.

Currently I am using a combination of ExamineDirectory with the usual filter "*.*", then using RegExps to check the extensions.

I know that in ExamineDirectory you can specify an extenstion such as "*.mp3", but I'm wondering if there is a way to specify multiple.

I tried "*.mp3;*.wav" (as used in FileRequesters()) and some similar formats, but none work.

Not a big deal, it would just remove the need to generate a bunch of file extension RegExps and to check them against hundreds of files that WON'T match.

Any ideas?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Multiple file extensions in ExamineDirectory() ?

Post by ts-soft »

Code: Select all

Directory$ = "C:\"
If ExamineDirectory(0, Directory$, "*.*")  
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      Ext$ = UCase(GetExtensionPart(DirectoryEntryName(0)))
      Select Ext$
        Case "MP3", "FLAC", "WAV"
      EndSelect
    EndIf

  Wend
  FinishDirectory(0)
EndIf
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: Multiple file extensions in ExamineDirectory() ?

Post by kenmo »

That's the first way I was doing it. Currently I use something like this:

Code: Select all

CreateRegularExpression(0, "^.*\.(chm|exe)$")

If ExamineDirectory(0, GetCurrentDirectory(), "*.*")
  While NextDirectoryEntry(0)
    Name.s = DirectoryEntryName(0)
    If MatchRegularExpression(0, LCase(Name))
      Debug "Found: " + Name
    EndIf
  Wend
  FinishDirectory(0)
EndIf
They both work fine. I just thought that maybe if I could filter the files I want within ExamineDirectory(), neither of these would be needed.

I think this has been discussed before, and there wasn't really a simpler way to do it than these two methods.....
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by Rook Zimbabwe »

would not the | character work as it does in the open or save file dialogs???

TESTING

Code: Select all

Directory$ = "C:\"   ; Lists all entries in the Windows directory (without the contents of sub-directories)
Dim Pattern.s(2)
Pattern(0) = "*."
Pattern(1) = "*.txt"
Pattern(2) = "*.*"

For xyz = 0 To 2
  If ExamineDirectory(0, Directory$, Pattern(xyz))  
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Type$ = " [File] "
      Else
        Type$ = " [Sub-Dir] "
      EndIf
      
      Debug DirectoryEntryName(0) + Type$ + "- Size in byte: " + Str(DirectoryEntrySize(0))
    Wend
    FinishDirectory(0)
  EndIf
Next
seems to work... checking again!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by IdeasVacuum »

Nope, can't get it to work here (XP x86 PB4.51)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by IdeasVacuum »

Using the For-Next loop works though, which is the next best thing :)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: Multiple file extensions in ExamineDirectory() ?

Post by kenmo »

I guess I'll just stick with my RegExp method :)

Thanks guys.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by Rook Zimbabwe »

sorry Ideasvacuum... I had to recode original posting... hope the clarified code worked! :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by IdeasVacuum »

Works very well Rook, with no perceivable lag time for my purposes. Funny how Kenmo brought up the subject just as I needed to find a solution. It would be nice if a multi-pattern could be used of course, since you should then end-up with a list of files in the same order as in Windows, without a subsequent sort of your own.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by TerryHough »

Why couldn't you work something like

Code: Select all

SetCurrentDirectory("C:\folder\")
Type.l = #PB_Explorer_AutoSort | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange                 
ExplorerListGadget(0, 10, 10, 380, 180,  GetCurrentDirectory() + "*.dll;*.exe;*.com",  Type)
into the ExamineDirectory() recursion? Then you just process the files in that list.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: Multiple file extensions in ExamineDirectory() ?

Post by C64 »

Rook's solution is far too slow for large directories, slow devices (DVD drives) and low-bandwidth network shares; because it basically does a whole directory scan for each extension required. You should only do one single directory scan, and check the extension for each file found during the scan. Kenmo's solution is MUCH faster.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by Rook Zimbabwe »

yep... but I wasn't looking for speed... I like Terrys idea though... and you don't have to scan for *. cause that would ONLY show folders (directories!) :mrgreen: nor the *.* but if you wanted to scan for SPECIFIC file types... like say *.txt and *.rtf and used terry's ignore codes... it might be fast enough to be a moot point! :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by TerryHough »

Actually, thanks to idle, we already have the solution.

Look here.
http://www.purebasic.fr/english/viewtop ... +directory

Code needs a slight update to run under PB 4.51

Code: Select all

Procedure Findfiles(*fn,StartDirectory.s,pattern.s,Recursive=1)
  Static strFiles.s,ct1,ptc,bPattern
  
  Protected k,ct 
  If Not bPattern 
    tpattern.s = RemoveString(pattern,"*")
    ptc = CountString(tpattern,"|")
    Static Dim pt.s(20)
    For k=1 To ptc+1
      pt(k-1) = StringField(tpattern,k,"|")
      Debug pt(k-1)
    Next
    bPattern = #True 
  EndIf 
  
  mDir = ExamineDirectory(#PB_Any, StartDirectory, "*.*") 
  If mDir 
    While NextDirectoryEntry(mDir)
      If DirectoryEntryType(mDir) = #PB_DirectoryEntry_File
        Directory$ = StartDirectory
        FileName.s = DirectoryEntryName(mDir)
        ct=0
        While ct <= ptc   
          If FindString(FileName,pt(ct), 1)
            FullFileName.s = StartDirectory + "\" + FileName
            If CallFunctionFast(*fn, @FullFileName) = 0 
              Break 2    
            EndIf    
            Ct1 + 1
          EndIf
          ct+1 
        Wend    
      Else
        tdir$ = DirectoryEntryName(mDir)
        If tdir$ <> "." And tdir$ <> ".."
          If Recursive = 1
            Findfiles(*fn,StartDirectory + "\" + tdir$,"")
          EndIf
        EndIf
      EndIf
    Wend
    FinishDirectory(mDir)
  EndIf
  
  ProcedureReturn ct1 
  
EndProcedure

Global NewList myfiles.s()
Global Msg$

Procedure MyCallBack(ResultString.s)
  Msg$ + ResultString + " "
  ProcedureReturn #True 
EndProcedure

count = FindFiles(@MyCallBack(),"C:\folder", "*.exe|*.dll|*.txt",1)

MessageRequester("Find Files", Msg$ + #LF$ + Str(count) + " files", #MB_ICONINFORMATION) 
NOTE: The above routine is case sensitive. For example: Program.EXE will not match *.exe. So, a code modification will be required.
Last edited by TerryHough on Tue Feb 22, 2011 3:21 pm, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Multiple file extensions in ExamineDirectory() ?

Post by IdeasVacuum »

...wow that is fast! :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Multiple file extensions in ExamineDirectory() ?

Post by MachineCode »

Doesn't work if I specify "C:\" as the root folder, but it seems to be searching?

Code: Select all

count = FindFiles(@MyCallBack(),"C:\", "*.exe|*.dll|*.txt",1)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply