ExamineDirectory()

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

ExamineDirectory()

Post by IdeasVacuum »

What is the Windows API equivalent to PB's ExamineDirectory()?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: ExamineDirectory()

Post by Michael Vogel »

FindFirstFile, I believe...

Code: Select all

Path.s = "C:\Windows\"
Name.s = Path + "w*.*"
SearchHandle = FindFirstFile_(Name, Info.WIN32_FIND_DATA)
If SearchHandle <> #INVALID_HANDLE_VALUE
	Repeat
		Entry.s = PeekS(@Info\cFileName[0])
		If Info\dwFileAttributes&#FILE_ATTRIBUTE_DIRECTORY=#Null
			Debug Entry
		EndIf
	Until FindNextFile_(SearchHandle,Info) = #False
	FindClose_(SearchHandle)
EndIf
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ExamineDirectory()

Post by IdeasVacuum »

Excellent, thank you Michael 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: ExamineDirectory()

Post by Dude »

Nice code, Michael... but can this work recursively if we just use a root path instead?
User avatar
Pierre Bellisle
User
User
Posts: 35
Joined: Wed Jun 27, 2018 5:12 am

Re: ExamineDirectory()

Post by Pierre Bellisle »

Here is one way. It is doing subfolders.
I avoided a recursive procedure because code execution will slow down as folder count grows.
A while/Wend is faster.

Depending on the wanted result, one can look at "localized filename", "symbolic link (aka soft link)", "hard link", junction, "Wow64 File system redirection", "short and long filename".

Code: Select all

EnableExplicit

If OpenConsole()
  Dim sSubFolder.s(0)             ;Array of subfolder names
  Define.s sFolder                ;Folder
  Define.s sFile                  ;File
  Define.s sFilter                ;Filespec WildCard 
  Define.i FileCount              ;Total files found
  Define.i FolderCount            ;Total folders found
  Define.i SubFolderLevel         ;Subfolder level index
  Define.i hFile                  ;Find file handle
  Define.WIN32_FIND_DATA FileData ;Find file handle  
  
  sFolder = "C:\Windows\Boot\" ;Need an ending backslash
  sFilter = "*.*"              ;Filespec WildCard for files only, folders are intentionally excluded
  
  While #True
    hFile = FindFirstFile_(sFolder + "*", FileData) ;Get all files and folders including "." and ".."
    If hFile <> #INVALID_HANDLE_VALUE ;Make sure we have a valid handle
      Repeat ;Get everything
        sFile = PeekS(@FileData\cFileName[0]) ;Copy data to a string
        If FileData\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY ;If it's a folder
          If sFile <> "." And sFile <> ".." ;Check For "." And "..", only those can end With a dot.
            FolderCount = FolderCount + 1 ;Increment folder count
            SubFolderLevel = SubFolderLevel + 1 ;Increment sub folder index
            ReDim sSubFolder(SubFolderLevel) ;Make room to store it
            sSubFolder(SubFolderLevel) = sFolder + sFile + "\" ;Put sub folder name in array
            ConsoleColor(6, 0)
            PrintN("Folder: " + sSubFolder(SubFolderLevel) + sFile) ;Show subfolder name   
            ConsoleColor(7, 0)  
          EndIf    
        Else ;It's a file
          If PathMatchSpec_(sFile, sFilter) ;Get file according to sFilter filespec via PathMatchSpec API
            PrintN("File:   " + sSubFolder(SubFolderLevel) + sFile) ;Show file name 
            FileCount = FileCount + 1 ;Increment file count
          EndIf              
        EndIf  
      Until FindNextFile_(hFile,FileData) = #False ;Do as long as there is data
      FindClose_(hFile) ;Clean up this handle
    EndIf
    If SubFolderLevel = 0 ;All sublevel are done
      Break               ;So exit
    EndIf  
    sFolder = sSubFolder(SubFolderLevel) ;Point to a new subfolder
    SubFolderLevel = SubFolderLevel - 1  ;Going up one level
  Wend
  ConsoleColor(14, 0)
  PrintN("Foldercount: " + Str(Foldercount)) 
  PrintN("Filecount:   " + Str(FileCount))
  ConsoleColor(7, 0)
  
  FindClose_(hFile)

  Input()
  CloseConsole()
     
EndIf
Post Reply