Manage Learn to apply best practices and optimize your operations.

How To Determine If A Drive, Path Or File Exists ( Using Windows-Api)


Const 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

'remove training slash before verifying
sFolder = UnQualifyPath(sFolder)

'call the API pasing the folder
hFile = FindFirstFile(sFolder, WFD)

'if a valid file handle was returned,
'and the directory attribute is set
'the folder exists
FolderExists = (hFile <> INVALID_HANDLE_VALUE) And (WFD.dwFileAttributes
And FILE_ATTRIBUTE_DIRECTORY)
If FolderExists = 16 Then
FolderExists = True
Else
FolderExists = False
End If
'clean up
Call FindClose(hFile)

End Function

Function UnQualifyPath(Byval sFolder As String) As String

'trim and remove any trailing slash
sFolder = Trim$(sFolder)

If Right$(sFolder, 1) = "\" Then
UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
Else
UnQualifyPath = sFolder
End If

End Function

Dig Deeper on Lotus Notes Domino Administration Tools

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close