|
-
Jul 21st, 2001, 08:38 PM
#1
Thread Starter
Member
check existence of a file
How do I check to see if a file or folder exists?
-
Jul 21st, 2001, 09:07 PM
#2
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
-
Jul 21st, 2001, 09:11 PM
#3
PowerPoster
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|