Die Lösung von Ulrich stammt wohl ursprünglich von VBA (wegen vbNormal etc.) und ist ohne Option Declare, aber ist schließlich auch schon älter.
Bzw. sehr alt, wenn man auf "Letzte Änderung" schaut:
« Letzte Änderung: 01.01.70 - 01:00:00 von 1034200800 »
;D
Ich habe aber genau sowas heute gebraucht, und leicht modifiziert.
Public Function ExistsWindowsDirFile(strWinFilePath As String) As Integer
%REM
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Purpose:
Checks if a directory or file exists.
-----------------------------------------------------------------------------------------------------
Returns:
True if the provided file or directory exists and False if not.
-----------------------------------------------------------------------------------------------------
Used functions/subs:
* ErrorMessage -- a standard error handling procedure.
-----------------------------------------------------------------------------------------------------
Example:
Sub Click(Source As Button)
Dim strRetInputbox As String
Dim intFunctionRet As Integer
strRetInputbox = Inputbox$("Enter path of directory or complete path of a file:")
If strRetInputbox = "" Then Exit Sub
intFunctionRet = ExistsWindowsDirFile(strRetInputbox)
If (intFunctionRet = True) Then
Msgbox "Provided directory or file does exist :-)", 64
Else
Msgbox "File or directory not found !", 48
End If
End Sub
-----------------------------------------------------------------------------------------------------
History:
Feb 17, 2002 Ulrich Krause New
Feb 27, 2005 Matthias TMC Several changes
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
%END REM
On Error Goto ErrorHandler
Dim intReturn As Integer
'// Prepare source string
strWinFilePath = Lcase$(Trim$(strWinFilePath))
'// Add colon and/or back slash (if missing and if provided path is a drive)
If Len(strWinFilePath) = 1 Then
Select Case strWinFilePath
Case "a" To "z": strWinFilePath = strWinFilePath & ":\"
Case Else: Exit Function 'False will be returned
End Select
Elseif Len(strWinFilePath) = 2 Then
Select Case strWinFilePath
Case "a:" To "z:": strWinFilePath = strWinFilePath & "\"
Case Else: Exit Function 'False will be returned
End Select
End If
'// The trick. A runtime-error #53 will occur if dir/file cannot be found.
intReturn = Getfileattr(strWinFilePath)
'// No runtime-error occured, so we return True (because now we can assume that dir/file exists)
ExistsWindowsDirFile = True
GoOut:
Exit Function
ErrorHandler:
Select Case Err
Case 53: ' 53 = File not found
ExistsWindowsDirFile = False
Resume GoOut
Case Else:
ExistsWindowsDirFile = False
ErrorMessage "ExistsWindowsDirFile"
Resume GoOut
End Select
End Function