TGC Codebase Backup



Listing files and folders by IanM

5th Dec 2003 20:39
Summary

Get a list of files or folders



Description

Allows you to get a list of the files and folders in the current directory.

Included features:
Search all folders within the specified folder, or not
Extract all folder names into the list, or not
Extract all file names into the list, or not
Ready to 'include' correctly, despite the arrays it uses :)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ScanDirectory("C:\\Program Files\\Dark Basic Software\\Dark Basic Professional\\Tools", SCAN_STANDARD)

for i=0 to array count( FileList() )
   print FileList(i).RelativeName; " - "; FileList(i).Size; " - "; FileList(i).Date
next i
wait key
end



`------------------LIBRARY STARTS HERE ----------------------

` These constants are used to make up the 'mode' of the ScanDirectory function.
` Just add together the bits that you want to switch off ... for example to stop
` searching folders with the named folder, and to not list any files you would use
`  SCAN_NORECURSE + SCAN_NOFILES.

#constant SCAN_STANDARD          0
#constant SCAN_NORECURSE         1
#constant SCAN_NOFILES           2
#constant SCAN_NODIRS            4
#constant SCAN_NOSLASH           8
#constant SCAN_NOSIZE            16


type FileInfo_t
   Name as string
   RelativeName as string
   IsFolder as integer
   Date as string
   Size as dword
endtype


global dim FileList() as FileInfo_t


function ScanDirectory(SourceDir as string, Mode as dword)
   local CurrentDir as string

   undim FileList()
   global dim FileList() as FileInfo_t
   empty array FileList()

   if SourceDir = "" then SourceDir = "."

   if path exist( SourceDir )
      CurrentDir = get dir$()

      set dir SourceDir
      ScanDirectoryRecurse( get dir$(), "", Mode )

      set dir CurrentDir
   endif
endfunction


function ScanDirectoryRecurse( CurrentPath as string, RelativePath as string, Mode as dword )
   local CurrentFile as FileInfo_t

   local dim Folders() as FileInfo_t
   empty array Folders()

   set dir CurrentPath

   find first
   while get file type() >= 0

      CurrentFile.Name           = get file name$()
      CurrentFile.RelativeName   = RelativePath + CurrentFile.Name
      CurrentFile.IsFolder       = get file type()
      CurrentFile.Date           = get file date$()

      if (Mode && SCAN_NOSIZE) = 0
         CurrentFile.Size = file size( CurrentFile.Name )
      else
         CurrentFile.Size = 0
      endif

      if CurrentFile.IsFolder = 1

         if CurrentFile.Name <> "." then if CurrentFile.Name <> ".."
            array insert at bottom Folders()
            Folders() = CurrentFile
         endif

      else

         if (Mode && SCAN_NOFILES) = 0
            array insert at bottom FileList()
            FileList() = CurrentFile
            if (Mode && SCAN_NOSIZE) = 0 then FileList().Size = file size( FileList().Name )
         endif

      endif

      find next
   endwhile

   array index to top Folders()
   while array index valid( Folders() )

      if (Mode && SCAN_NODIRS) = 0
         array insert at bottom FileList()
         FileList() = Folders()
         if (Mode && SCAN_NOSLASH) = 0 then FileList().RelativeName = FileList().RelativeName + "\\"
      endif

      if (Mode && SCAN_NORECURSE) = 0
         ScanDirectoryRecurse( CurrentPath + "\\" + Folders().Name, Folders().RelativeName + "\\" , Mode )
      endif

      next array index Folders()

   endwhile
endfunction