Quick n dirty dir to file

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Quick n dirty dir to file

Post by Fangbeast »

This is just a quick and dirty directory or files list to text file. Found myself wanting a text file list of all files or directories in a text file and although I know DIR can do it, it was worth learning the pb way as it can be extended.

All 3 routines could be concatenated but I find them more useful (and easier to read). Do what you want with it.

Code: Select all

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

Global NewList FoundDirs.s()

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

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

;============================================================================================================================
; Find a file on disk and save matches to a text file.
;============================================================================================================================

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

;============================================================================================================================
;
;============================================================================================================================

UpdateDirs("C")           ; Create the text file list of directories only
UpdateFiles("C:\")        ; Create the text file list of files only
FindFile(".log", "C:")    ; ; Find a file on disk and save matches to a text file.