Results 1 to 7 of 7

Thread: [RESOLVED] Check network path exists

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] Check network path exists

    Hi all,

    I have searched but can't find an answer to this.

    If I have a string containing a file path. eg "R:\Administration\Timekeeping\test.xls" How do I check that the path exists?

    If the computer running the app is not connected to the network, and hence the path doesn't exist, then using this code

    Code:
    Public Function FileExists(strFilePath As String) As Boolean
        FileExists = False 'Assume the worst!
        'Make sure a filename was passed!
        If Len(strFilePath) > 0 Then
                  'Check to see if the file exists!
                  If Len(Dir$(strFilePath)) > 0 Then
                      'The file exists!
                      FileExists = True
                  End If
        End If
    End Function
    If Len(Dir$(strFilePath)) > 0 Then

    still creates an error "bad file name or number"

    Can someone suggest a way of finding out whether or not a path exists?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check network path exists

    You are correct, and there is no code in the world that will tell you the path exists if it doesn't. And, it doesn't exist for a particular machine if said machine is not connected to the network with the drive mapped and/or available to it.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Check network path exists

    I've just modified the FileExists Function slightly and it now appears to work.
    Out of interest. Can this snippet of code now be significantly reduced? Got the feeling it can but I'm no expert and it might take some one a second to say it can while I'd spend a lot of time in trial and error to say it can't.

    Code:
    Public Function FileExists(strFilePath As String) As Boolean
       On Error GoTo FileExists_Error
        FileExists = False 'Assume the worst!
        'Make sure a filename was passed!
        If Len(strFilePath) > 0 Then
            'Check to see if the file exists!
            If Len(Dir$(strFilePath)) > 0 Then
                'The file exists!
                FileExists = True
            End If
        End If
       On Error GoTo 0
       Exit Function
    FileExists_Error:
        If Err.Number = 52 Then
            FileExists = False
        Else
            MsgBox "Error " & Err.Number & " (" & Err.Description & ") on line " & Erl & " in FileExists of Module1"
        End If
    End Function

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check network path exists

    Providing you have a network connection, that should work just fine.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Check network path exists

    Wouldn't these work?

    ' file check
    Code:
    Public Function bFileExists(sFile As String) As Boolean
        On Error Resume Next
        bFileExists = ((GetAttr(sFile) And vbDirectory) = 0)
    End Function
    ' folder check
    Code:
    Public Function bDirExists(sDir As String) As Boolean
        On Error Resume Next
        bDirExists = ((GetAttr(sDir) And vbDirectory) <> 0)
    End Function

  6. #6
    New Member
    Join Date
    Aug 2010
    Posts
    3

    Re: [RESOLVED] Check network path exists

    this is my clsFileFolder class

    Code:
    Option Explicit
    
    Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    
    Private Const OFS_MAXPATHNAME = 128
    Private Const OF_EXIST = &H4000
    
    Private Type OFSTRUCT
        cBytes As Byte
        fFixedDisk As Byte
        nErrCode As Integer
        Reserved1 As Integer
        Reserved2 As Integer
        szPathName(OFS_MAXPATHNAME) As Byte
    End Type
    
    Public Function CheckNetworkExist(strFullPath As String) As Boolean
    
        Dim strResult
        Dim strucFname As OFSTRUCT
        
        If Left(strFullPath, 2) = "\\" Then
            strResult = OpenFile(strFullPath, strucFname, OF_EXIST)
            If strResult <> -1 Then
                CheckNetworkExist = True
            Else
                CheckNetworkExist = False
            End If
        End If
        
    End Function

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: Check network path exists

    Quote Originally Posted by Hack View Post
    You are correct, and there is no code in the world that will tell you the path exists if it doesn't. [...]
    Sorry, can't resist

    Code:
    Public Function PathExists(path As String) As Boolean
        PathExists = True
    End Function

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