Page 1 of 1

Reusable file finder, dir lister

Posted: Thu Apr 26, 2007 2:36 pm
by Fangbeast
I know recursive listers have been done to death but this is useful to me so...

The below routine uses the same recursive drive lister that someone made in the forum a couple of years ago to do three different things without too much extra code. Most of my apps need to list files, folders or find specific files so this cuts down on code overhead.

UpdateDirs("C") will create a text file on disk with the list of every unique directory on drive c.

UpdateFiles("C:\") will create a text file on disk with the list of all files found with full paths

FindFile(".log") will create a text file on disk with the list of all files found that match ".log" with full path and filename.

Code: Select all

;============================================================================================================================
; Define the global list and other variables we might be needing
;============================================================================================================================

Global NewList FoundDirs.s(), FileId.l

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

Procedure SearchEngine(SearchDir.s, Function.l, FileString.s)
  ClearList(FoundDirs.s())
  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() + "\"
        If Function.l = 1                                       ; Are we updating the database of directories
          WriteStringN(FileId.l, Path)
        EndIf
        While NextDirectoryEntry(0)
          Filename.s = DirectoryEntryName(0)
          Select DirectoryEntryType(0)
            Case 1
              If Function.l = 2                                 ; Are we saving the found files to a text file on disk?
                WriteStringN(FileId.l, Path + Filename)
              EndIf
              If Function.l = 3
                If FindString(Filename, FileString.s, 1) <> 0   ; Are we showing a list of matches to the user?
                  WriteStringN(FileId.l, Path + Filename)
                EndIf
              EndIf
            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

;============================================================================================================================
; Create the text file list of directories only
;============================================================================================================================

Procedure UpdateDirs(Drive.s)
  DriveLetter.s = Left(Drive.s, 1)
  Drive.s = DriveLetter.s + ":"
  FileId.l = CreateFile(#PB_Any, "c:\" + DriveLetter.s + " dirlist.txt")
  If FileId.l <> 0
    SearchEngine(Drive.s, 1, "")
    CloseFile(FileId.l)
    If FileId.l <> 0
      FileId.l = 0
    EndIf
  EndIf
EndProcedure

;============================================================================================================================
; Create the text file list of files only
;============================================================================================================================

Procedure UpdateFiles(Drive.s)
  DriveLetter.s = Left(Drive.s, 1)
  Drive.s = DriveLetter.s + ":"
  FileId.l = CreateFile(#PB_Any, "c:\" + DriveLetter.s + " filelist.txt")
  If FileId.l <> 0
    SearchEngine(Drive.s, 2, "")
    CloseFile(FileId.l)
    If FileId.l <> 0
      FileId.l = 0
    EndIf
  EndIf
EndProcedure

;============================================================================================================================
; Find a file on disk and display it
;============================================================================================================================

Procedure FindFile(FileString.s)
  SearchEngine("C:", 3, FileString.s)
  DriveLetter.s = Left(Drive.s, 1)
  Drive.s = DriveLetter.s + ":"
  FileId.l = CreateFile(#PB_Any, "c:\" + DriveLetter.s + " foundfiles.txt")
  If FileId.l <> 0
    SearchEngine(Drive.s, 2, "")
    CloseFile(FileId.l)
    If FileId.l <> 0
      FileId.l = 0
    EndIf
  EndIf
EndProcedure