In diesem Tip möchte ich euch vorstellen, wie man unter Zuhilfenahme einiger Windows API Calls ermittelt, ob ein Laufwerk, ein Ordner oder eine Datei existiert.
wie immer gibt es den abgedruckten Quelltext auch unter
http://www.eknori.de/downloads/DriveFolderFileExist.zipConst INVALID_HANDLE_VALUE = -1
Const MAX_PATH = 260
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (Byval lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (Byval hFindFile As Long) As Long
Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (Byval nBufferLength As Long, Byval lpBuffer As String) As Long
Sub Click(Source As Button)
Msgbox DriveExists("Z")
Msgbox FolderExists("c:\temp")
Msgbox FileExists("c:\temp\unzip.exe")
End Sub
Function FileExists(sSource As String) As Variant
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sSource, WFD)
FileExists = hFile <> INVALID_HANDLE_VALUE
Call FindClose(hFile)
End Function
Function DriveExists(drvName As String) As Variant
Dim allDrives As String
allDrives = Space$(64)
Call GetLogicalDriveStrings(Len(allDrives), allDrives)
DriveExists = Instr(1, allDrives, drvName, 1) > 0
End Function
Function FolderExists(sFolder As String) As Variant
Dim hFile As Long
Dim WFD As WIN32_FIND_DATA
sFolder = UnQualifyPath(sFolder)
hFile = FindFirstFile(sFolder, WFD)
FolderExists = (hFile <> INVALID_HANDLE_VALUE) And (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
If FolderExists = 16 Then
FolderExists = True
Else
FolderExists = False
End If
Call FindClose(hFile)
End Function
Function UnQualifyPath(Byval sFolder As String) As String
sFolder = Trim$(sFolder)
If Right$(sFolder, 1) = "\" Then
UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
Else
UnQualifyPath = sFolder
End If
End Function