list all dirs and files

Just starting out? Need help? Post your questions and find answers here.
Fable Fox
User
User
Posts: 29
Joined: Fri Dec 12, 2003 4:52 am

list all dirs and files

Post by Fable Fox »

I want to list all dirs and files on a cd-rom inside a listbox and save it as text, have no problem except getting all dirs and files.

i know there are tow ways, recursive and link list. i tried recursive but getting stack overflow, even for cd-rom without much dirs or files. before i try the link list, is there any easier or better way, have someone done things like this before so i can learn from ? Thanks.


http://www.fablefox.com <-- The Fable of Fox
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: list all dirs and files

Post by Fangbeast »

Fable Fox wrote:I want to list all dirs and files on a cd-rom inside a listbox and save it as text, have no problem except getting all dirs and files.

i know there are tow ways, recursive and link list. i tried recursive but getting stack overflow, even for cd-rom without much dirs or files. before i try the link list, is there any easier or better way, have someone done things like this before so i can learn from ? Thanks.


http://www.fablefox.com <-- The Fable of Fox

Code: Select all

;============================================================================================================================
; Universal, recursive search engine used by many functions
;============================================================================================================================

Procedure SearchEngine(SearchDir.s)
  ClearList(FoundDirs.s())                                  ; Clear the list of found directories
  If SearchDir.s <> ""
    If Right(SearchDir.s, 1) = ""
      SearchDir.s = Left(SearchDir.s, Len(SearchDir.s) - 1)
    EndIf
    AddElement(FoundDirs.s())
    FoundDirs.s() = SearchDir.s
    Index = 0
    Repeat
      SelectElement(FoundDirs.s(), Index)
      If ExamineDirectory(0, FoundDirs.s(), "*.*")
        Path.s = FoundDirs.s() + ""
        While NextDirectoryEntry(0)
          Filename.s = DirectoryEntryName(0)
          Select DirectoryEntryType(0)
            Case 1                                         ; A filename has been found in the current dir
              FullFileName.s = Path.s + FileName.s          ; Now do whatever you want with the file name
                                                            ; Send it to a console, a list, anywhere you like
            Case 2
              Filename.s = DirectoryEntryName(0)
              If Filename.s <> ".." And Filename.s <> "."
                AddElement(FoundDirs())
                FoundDirs() = Path + Filename.s
              EndIf
          EndSelect
        Wend
      EndIf
      Index + 1
    Until Index > CountList(FoundDirs()) -1
  EndIf
EndProcedure


SearchEngine("C:")     ; Just test it now
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
bcgreen
User
User
Posts: 33
Joined: Sun Nov 02, 2003 7:33 am
Location: Pullman, WA

Post by bcgreen »

Hmmm...that doesn't appear to be a recursive procedure; it never calls itself...
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Here's what I've always used:

Code: Select all

Global raw,recurse
Global Dim raw$(99999)

Procedure GetRaw(dir$,dir)
  tmp=dir+1
  If ExamineDirectory(tmp,dir$,"*.*")
    While NextDirectoryEntry(tmp)
      type=DirectoryEntryType(tmp)
      name$=DirectoryEntryName(tmp)
      If type=2 ; Folder.
        If recurse=1 And name$<>"." And name$<>".."
          GetRaw(dir$+name$+"\",tmp)
        EndIf
      Else ; File.
        raw+1 : raw$(raw)=dir$+name$
      EndIf
    Wend
    FinishDirectory(tmp)
  EndIf
  SortArray(raw$(),2,1,raw) ; Ascending alphabetical.
EndProcedure

recurse=1 : GetRaw("c:\",0)
For a=1 To raw : Debug raw$(a) : Next
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply