ExamineDirectory()
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
ExamineDirectory()
What is the Windows API equivalent to PB's ExamineDirectory()?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- Michael Vogel
- Addict
- Posts: 2680
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: ExamineDirectory()
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
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: ExamineDirectory()
Excellent, thank you Michael
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: ExamineDirectory()
Nice code, Michael... but can this work recursively if we just use a root path instead?
- Pierre Bellisle
- User
- Posts: 35
- Joined: Wed Jun 27, 2018 5:12 am
Re: ExamineDirectory()
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".
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