Find all files in one folder (or list folders in subfolder)

Just starting out? Need help? Post your questions and find answers here.
omid-xp
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Jan 27, 2004 2:17 pm

Find all files in one folder (or list folders in subfolder)

Post by omid-xp »

I know this topic exist before this but i can't find my answer in its.

1. I want find all files in one folder
2. I want find list folders in one subfolder.

how can i do this?
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Find all files in one folder (or list folders in subfold

Post by Fangbeast »

omid-xp wrote:I know this topic exist before this but i can't find my answer in its.

1. I want find all files in one folder
2. I want find list folders in one subfolder.

how can i do this?
Try this. Highlight one of the folders in the tree, type in what you are searching for in the search box below and press ENTER. Modify the code if you just want to show folders and not search for files. (I made this last night for a friend based on others code) :):)

Code: Select all

; Cute bubble tips

Procedure BubbleTip(bWindow.l, bGadget.l, bText.s)
  ToolTipControl = CreateWindowEx_(0, "ToolTips_Class32", "", $D0000000|$40, 0, 0, 0, 0, WindowID(bWindow), 0, GetModuleHandle_(0), 0)
  SendMessage_(ToolTipControl, 1044, 0, 0)
  SendMessage_(ToolTipControl, 1043, $DFFFFF, 0)
  SendMessage_(ToolTipControl, 1048, 0, 180)
  Button.TOOLINFO\cbSize = SizeOf(TOOLINFO)
  Button\uFlags = $11
  Button\hWnd = GadgetID(bGadget)
  Button\uId = GadgetID(bGadget)
  Button\lpszText = @bText
  SendMessage_(ToolTipControl, $0404, 0, Button)
EndProcedure

; Window Constants

Enumeration
  #Window_qsearch
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

; Gadget Constants

Enumeration
  #Gadget_qsearch_dirlist
  #Gadget_qsearch_filelist
  #Gadget_qsearch_searchframe
  #Gadget_qsearch_searchbox

  #Shortcut_qsearch_enter
  
  #Splitter_qsearch_vertical
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

; The main window

Procedure.l Window_qsearch()
  If OpenWindow(#Window_qsearch,175,0,747,457,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible,"Qsearchview 0.00")
    AddKeyboardShortcut(#Window_qsearch, #PB_Shortcut_Return, #Shortcut_qsearch_enter)
    If CreateGadgetList(WindowID(#Window_qsearch))
      ExplorerTreeGadget(#Gadget_qsearch_dirlist,10,10,200,385,"")
        BubbleTip(#Window_qsearch,#Gadget_qsearch_dirlist,"Highlight a drive or directory where you want the search to start")
      ListIconGadget(#Gadget_qsearch_filelist,220,10,515,440,"Location",370,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        AddGadgetColumn(#Gadget_qsearch_filelist,1,"Filename",120)
        BubbleTip(#Window_qsearch,#Gadget_qsearch_filelist,"This is where the list of files will be returned from the search")
      Frame3DGadget(#Gadget_qsearch_searchframe,10,400,728,50,"Search For:")
      StringGadget(#Gadget_qsearch_searchbox,20,420,180,20,"")
        BubbleTip(#Window_qsearch,#Gadget_qsearch_searchbox,"Type in here what you are looking for and press ENTER when ready. There is no need to use wildcard characters.")
      HideWindow(#Window_qsearch,0)
      ProcedureReturn WindowID()
    EndIf
  EndIf
EndProcedure

; Create a list to hold the dirs/files being found

NewList dir.s()

; Main program loop

If Window_qsearch()

; Add the single splitter

  SplitterGadget(#Splitter_qsearch_vertical,  10,  10,  726,  386,  #Gadget_qsearch_dirlist,  #Gadget_qsearch_filelist,  #PB_Splitter_Vertical | #PB_Splitter_Separator)
  SetGadgetState(#Splitter_qsearch_vertical,  186)
  
  quitqsearch = 0  ; Main window detect value for close
    
  ActivateWindow()  ; Set focus to main window
    
  Repeat
    EventID = WaitWindowEvent()
    Select EventID
      Case #PB_Event_CloseWindow
        If EventWindowID() = #Window_qsearch
          quitqsearch = 1
        EndIf
      Case #PB_Event_Menu
        Select EventMenuID()
          Case #Shortcut_qsearch_enter             : Gosub CheckEnter ; Is ENTER presses, start search
        EndSelect
      Case #PB_Event_Gadget
        Select EventGadgetID()
          Case #Gadget_qsearch_dirlist             : Gosub TreeClicked ; user clicked on a directory, get dir name
          Case #Gadget_qsearch_filelist
            Select EventType()
              Case #PB_EventType_LeftDoubleClick   : Gosub ListClicked ; User clicked on a found file, do something
              Case #PB_EventType_RightDoubleClick
              Case #PB_EventType_RightClick
              Default
            EndSelect
        EndSelect

    EndSelect
  Until quitqsearch
  CloseWindow(#Window_qsearch)
EndIf
End

;

TreeClicked:

  If GetGadgetState(#Gadget_qsearch_dirlist) <> -1
    ClearGadgetItemList(#Gadget_qsearch_dirlist)
    TreeItem.s = GetGadgetText(#Gadget_qsearch_dirlist)
  EndIf

Return

;

ListClicked:
Return

;

CheckEnter:

  If TreeItem.s <> ""
    FileSpec.s = LCase(GetGadgetText(#Gadget_qsearch_searchbox))
    If FileSpec.s <> ""
      FocusID = GetFocus_()
      Select FocusID
        Case GadgetID(#Gadget_qsearch_searchbox)
          Gosub SearchFiles
      EndSelect
      FileSpec.s = ""
      SetGadgetText(#Gadget_qsearch_searchbox, "")
    EndIf
  EndIf


Return

;

SearchFiles:

    ClearList(dir.s())
    
    defaultdrive.s = TreeItem.s
    
    If Right(defaultdrive.s, 1) = ""
      defaultdrive.s = Left(defaultdrive.s, Len(defaultdrive.s) - 1)
    EndIf
    
    AddElement(dir())
    
    dir() = defaultdrive.s
    
    idx = 0
    
    Repeat
      SelectElement(dir(), idx)
      If ExamineDirectory(0, dir(), "*.*")
        path.s = dir() + ""
        quit = 0
        Repeat
          nextfile = NextDirectoryEntry()
          filename.s = DirectoryEntryName()
          Select nextfile
            Case 0
              quit = 1
            Case 1
              count + 1
              While WindowEvent() : Wend
              If FindString(path.s +filename.s, FileSpec.s, 1)
                AddGadgetItem(#Gadget_qsearch_filelist, -1, path.s + Chr(10) + filename.s)
              EndIf
            Case 2
              filename.s = DirectoryEntryName()
              If filename.s <> ".." And filename.s <> "."
                AddElement(dir())
                dir() = path + filename.s
              EndIf
          EndSelect  
        Until quit = 1
      EndIf
      idx + 1
    Until idx > CountList(dir())-1
    
    TreeItem.s = ""
    
Return

Amateur Radio, D-STAR/VK3HAF
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

viewtopic.php?t=8359&highlight=matchpattern

see also the x_lib on my site for latest version
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Very nice

Post by Fangbeast »

blueznl wrote:viewtopic.php?t=8359&highlight=matchpattern

see also the x_lib on my site for latest version
I like your solution for complicated searches, lots of choices, very well done (And I can't think like that (ROFL).

However, The above code I gave is nice and simple for those who don't know anything about such searches and is more easily modifiable for beginners.

I might steal your logic (evil grin) to add extra pattern searching to my simple form if my friend who asked for it buys me a hamburger or two:):)
Amateur Radio, D-STAR/VK3HAF
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

if i did mind people stealing or using my code i wouldn't post it :-)

it started the same way as you did, when someone else asked a question on it... don't recall who asked it first, somwhere in the beginner area, then i wrote this up, then i expended it a few times...

i think it's reasonable fast, tried some other solutions but this was the fasted i could come up with... when dumping a full harddisk content speed matters :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Speeeeeeed!!!

Post by Fangbeast »

blueznl wrote:if i did mind people stealing or using my code i wouldn't post it :-)

it started the same way as you did, when someone else asked a question on it... don't recall who asked it first, somwhere in the beginner area, then i wrote this up, then i expended it a few times...

i think it's reasonable fast, tried some other solutions but this was the fasted i could come up with... when dumping a full harddisk content speed matters :-)
I had to whip the above code up after a friend asked me what happened to my original fast finder. he was 2 years too late. Did that in 15 minutes and he was drooling ove PB's speed and cursing Window Explorer's slowness.

He is now happy and off my back (at least for a while)
Amateur Radio, D-STAR/VK3HAF
omid-xp
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Jan 27, 2004 2:17 pm

Post by omid-xp »

Thanks Fangbeast for your source code.

Thanks blueznl for your help.
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

why not just use the intergrated filesystem lib in pb?
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

Let me enlighten you folks with the art of recurse programming.


Code: Select all

Procedure recurseDir(path.s,pat.s,void.l) ;void should be 0
DefType.s r,fileName:DefType.l t
If ExamineDirectory(void,path,"")
 Repeat
  t=NextDirectoryEntry():r=DirectoryEntryName()
  If (t=2) And (PeekB(@r)<>46) ;Because we do not need . & ..
   recurseDir((path+"\"+r),pat,(void+1))
   UseDirectory(void)
  ElseIf (t=1) And (UCase(GetExtensionPart(r))=UCase(pat))
   fileName=path+"\"+r ;fileName contains current filename + path
   PrintN(fileName)    ;You can insert an other code or procedure here
  EndIf
 Until t=0
EndIf
EndProcedure

If OpenConsole()
 Input()
 recurseDir("C:\Program Files","exe",0) ;will list *.exe in Program Files
 Input()
 CloseConsole()
EndIf
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Code: Select all

If (t=2) And (PeekB(@r)<>46) ;Because we do not need . & .. 
You might want to change that check a bit.
".hello" is a valid directory name.

Timo
quidquid Latine dictum sit altum videtur
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

Sure? Because on my OS (win98_se) it isn't
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Here on Win2k, it is valid.
quidquid Latine dictum sit altum videtur
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

OK then. For win2k an xp users should use this code

Code: Select all

Procedure recurseDir(path.s,pat.s,void.l) ;void should be 0
DefType.s r,fileName:DefType.l t
If ExamineDirectory(void,path,"")
 Repeat
  t=NextDirectoryEntry():r=DirectoryEntryName()
  If (t=2) And (r<>".") And (r<>"..") ;Because we do not need . & ..
   recurseDir((path+"\"+r),pat,(void+1))
   UseDirectory(void)
  ElseIf (t=1) And (UCase(GetExtensionPart(r))=UCase(pat))
   fileName=path+"\"+r ;fileName contains current filename + path
   PrintN(fileName)    ;You can insert an other code or procedure here
  EndIf
 Until t=0
EndIf
EndProcedure

If OpenConsole()
 Input()
 recurseDir("C:\Program Files","exe",0) ;will list *.exe in Program Files
 Input()
 CloseConsole()
EndIf
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

Question t Freak.

Because you have your on homepage and obviously know something about recurse programming maybe you gan give me some ideas. My current project is a chess program and I allready have made a move generator. The only thing to do know is to write AI but how do you tell the computer what move is the best?
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

just wanna say that ".hello" is a valid on my win98 se too

Best regards
Henrik
Post Reply