Hur tar man enklast reda på om en fil exsisterar? Alltså en funktion motsvarande fileExists i ASP. Jag brukar göra så här och tycker det är en rätt snygg lösning! Så här ser det ut i PDM:FileExists i vb
Är detta den enklaste lösningen?:
if dir(sökväg)<>"" then
'filen exsisterar
end if
mvh FransSv: FileExists i vb
Sample Code =====================================
Dim exist As Boolean
On Error Resume Next
exist = FileLen("c:\dinfil.fil") And True
On Error GoTo 0
Sample Code =====================================
Men säkert inte det bästa eller snabbaste.
/peterhSv: FileExists i vb
Public Const gstrSEP_DIR$ = "\" ' Directory separator character
Public Const gstrQUOTE$ = """"
'-----------------------------------------------------------
' FUNCTION: FileExists
' Determines whether the specified file exists
'
' IN: [strPathName] - file to check for
'
' Returns: True if file exists, False otherwise
'-----------------------------------------------------------
'
Public Function FileExists(ByVal strPathName As String) As Boolean
Dim intFileNum As Integer
On Error Resume Next
'
' If the string is quoted, remove the quotes.
'
strPathName = strUnQuoteString(strPathName)
'
'Remove any trailing directory separator character
'
If Right$(strPathName, 1) = gstrSEP_DIR Then
strPathName = Left$(strPathName, Len(strPathName) - 1)
End If
'
'Attempt to open the file, return value of this function is False
'if an error occurs on open, True otherwise
'
intFileNum = FreeFile
Open strPathName For Input As intFileNum
FileExists = (Err.Number = 0)
Close intFileNum
Err.Clear
End Function
Public Function strUnQuoteString(ByVal strQuotedString As String)
'
' This routine tests to see if strQuotedString is wrapped in quotation
' marks, and, if so, remove them.
'
strQuotedString = Trim$(strQuotedString)
If Mid$(strQuotedString, 1, 1) = gstrQUOTE Then
If Right$(strQuotedString, 1) = gstrQUOTE Then
'
' It's quoted. Get rid of the quotes.
'
strQuotedString = Mid$(strQuotedString, 2, Len(strQuotedString) - 2)
End If
End If
strUnQuoteString = strQuotedString
End Function