Find Folder

Originally published in NL #141
- StPendl StPendl

Find Folder | Folder Existence

Folder Existence

This demo shows how to use FindFirstFileA to query the existence of a folder.
folder$(1) = "C:\"
folder$(2) = "C:\WINDOWS\system*"
folder$(3) = "C:\WINDOWS\system?2"
folder$(4) = "C:\Program*"
folder$(5) = "C:\Program*\test"
folder$(6) = "C:\boot.ini"
 
for count = 1 to 6
    if FolderExistsEx(folder$(count), FolderName$, ShortName$) then
      print "Folder: "; chr$(34); folder$(count); chr$(34); " does exist."
        print "Found: "; chr$(34); FolderName$; chr$(34)
    print "Short: "; chr$(34); ShortName$; chr$(34)
    else
      print "Folder "; chr$(34); folder$(count); chr$(34); " does not exist."
    end if
 
print
next
end
 
function FolderExistsEx(Path$, byref FoundFolderName$, byref ShortFolderName$)
' checks for the existance of the given folder
'
' returns true(1) or false(0)
'
' FoundFolderName$ will hold the first found name when wildcards (? and *) are used
' ShortFolderName$ will hold the name in the 8.3 format
 
if right$(Path$,1) = "\" then Path$ = left$(Path$, len(Path$)-1)
if right$(Path$,1) = ":" then
    if instr(Drives$, lower$(Path$)) > 0 then
      FolderExistsEx = 1
      FoundFolderName$ = Path$
      ShortFolderName$ = Path$
      exit function
    end if
end if
 
Path$ = Path$ + chr$(0)
 
struct Win32FindData, _
FileAttributes as ulong, _
CreationTime.LowDateTime as ulong, _
CreationTime.HighDateTime as ulong, _
LastAccessTime.LowDateTime as ulong, _
LastAccessTime.HighDateTime as ulong, _
LastWriteTime.LowDateTime as ulong, _
LastWriteTime.HighDateTime as ulong, _
FileSizeHigh as ulong, _
FileSizeLow as ulong, _
Reserved0 as ulong, _
Reserved1 as ulong, _
FileName$ as char[260], _
AlternateFileName$ as char[14]
 
calldll #kernel32, "FindFirstFileA", _
  Path$ as ptr, _
  Win32FindData as struct,_
  handle as ulong
 
if handle <> _INVALID_HANDLE_VALUE then
  if _FILE_ATTRIBUTE_DIRECTORY and Win32FindData.FileAttributes.struct then
    FolderExistsEx = 1
    FoundFolderName$ = Win32FindData.FileName$.struct
    ShortFolderName$ = Win32FindData.AlternateFileName$.struct
  end if
 
  calldll #kernel32, "FindClose", _
    handle as ulong, _
    result as long
  end if
end function

Find Folder | Folder Existence