ExamineDirectory()
Posted: Mon Feb 15, 2016 2:42 pm
What is the Windows API equivalent to PB's ExamineDirectory()?
http://www.purebasic.com
https://www.purebasic.fr/english/
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
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
Code: Select all
Path.s = "C:\Windows\"
Name.s = Path + "t?unk*" ; Lower-case "t" only wanted.
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 ; Shows "Twunk_16.exe" and "Twunk_32.exe" :(
EndIf
Until FindNextFile_(SearchHandle,Info) = #False
FindClose_(SearchHandle)
EndIf
Code: Select all
If CompareMemoryString(@tmp$, PeekI(@Ext()), #PB_String_NoCase) = #PB_String_Equal
;If tmp$ = Ext()