How do I check to see if a file or folder exists?
Printable View
How do I check to see if a file or folder exists?
Use the Dir function.
VB Code:
Private Function IsDirExisting(ByVal sDir As String) As Boolean IsDirExisting = CBool(Len(Dir$(sDir, vbDirectory))) End Function Private Function IsFileExisting(ByVal sFile As String) As Boolean IsFileExisting = CBool(Len(Dir$(sFile))) End Function
Hi
Check out this prev post
http://www.vbforums.com/showthread.p...threadid=90730
u can use Err to return lots of file problems so look that up in Help
Example
VB Code:
Dim mstrFilePath As String 'File path Sub fleOpenFile(lstrFileName As String, lbytFileNum As Byte, lintLength As Integer) On Error GoTo ErrorFix mstrFilePath = App.Path & "\Sample" 'App path and subdir Open mstrFilePath & lstrFileName For Random As lbytFileNum Len = lintLength Exit Sub ErrorFix: If Err.Number = 76 Then'The subdirectory does not exist fleErrNoPath Resume End If If Err.Number = 53 Then'The file does not exist fleErrNoFile lstrFileName, lbytFileNum, lintLength Resume End If End Sub Sub fleErrNoPath() 'Make directory if it doesn't exist MkDir App.Path & "\Sample" End Sub Sub fleErrNoFile(lstrFileName As String, lbytFileNum As Byte, lintLength As Integer) 'Create file if it doesn't exist On Error GoTo ErrorFix2: Close Open mstrFilePath & lstrFileName For Random As lbytFileNum Len = lintLength Exit Sub ErrorFix2: If Err.Number = 76 Then fleErrNoPath Resume End If End Sub 'So, call by (this is pseudo code - add ur details) fileVar = FreeFile fleOpenFile "\MyFile.dat", fileVar, Len(MyUDTorVariable)
Regards
Stuart