Results 1 to 3 of 3

Thread: check existence of a file

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Posts
    55

    check existence of a file

    How do I check to see if a file or folder exists?

  2. #2
    Matthew Gates
    Guest
    Use the Dir function.


    VB Code:
    1. Private Function IsDirExisting(ByVal sDir As String) As Boolean
    2.     IsDirExisting = CBool(Len(Dir$(sDir, vbDirectory)))
    3. End Function
    4.  
    5. Private Function IsFileExisting(ByVal sFile As String) As Boolean
    6.     IsFileExisting = CBool(Len(Dir$(sFile)))
    7. End Function

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. Dim mstrFilePath As String 'File path
    2.  
    3. Sub fleOpenFile(lstrFileName As String, lbytFileNum As Byte, lintLength As Integer)
    4.  
    5. On Error GoTo ErrorFix
    6.     mstrFilePath = App.Path & "\Sample" 'App path and subdir
    7.     Open mstrFilePath & lstrFileName For Random As lbytFileNum Len = lintLength
    8.     Exit Sub
    9.    
    10. ErrorFix:
    11.     If Err.Number = 76 Then'The subdirectory does not exist
    12.         fleErrNoPath
    13.         Resume
    14.     End If
    15.    
    16.     If Err.Number = 53 Then'The file does not exist
    17.         fleErrNoFile lstrFileName, lbytFileNum, lintLength
    18.         Resume
    19.     End If
    20. End Sub
    21.  
    22. Sub fleErrNoPath()
    23.     'Make directory if it doesn't exist
    24.     MkDir App.Path & "\Sample"
    25. End Sub
    26.  
    27. Sub fleErrNoFile(lstrFileName As String, lbytFileNum As Byte, lintLength As Integer)
    28.     'Create file if it doesn't exist
    29.     On Error GoTo ErrorFix2:
    30.     Close
    31.     Open mstrFilePath & lstrFileName For Random As lbytFileNum Len = lintLength
    32.     Exit Sub
    33.  
    34. ErrorFix2:
    35.     If Err.Number = 76 Then
    36.         fleErrNoPath
    37.         Resume
    38.     End If
    39. End Sub
    40.  
    41. 'So, call by (this is pseudo code - add ur details)
    42.       fileVar = FreeFile
    43.       fleOpenFile "\MyFile.dat", fileVar, Len(MyUDTorVariable)

    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width