Page 1 of 1

list all dirs and files

Posted: Mon Apr 02, 2007 11:26 am
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

Re: list all dirs and files

Posted: Mon Apr 02, 2007 11:43 am
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

Posted: Fri May 25, 2007 3:03 am
by bcgreen
Hmmm...that doesn't appear to be a recursive procedure; it never calls itself...

Posted: Fri May 25, 2007 3:10 am
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