|
-
Feb 22nd, 2008, 01:37 PM
#1
Thread Starter
Fanatic Member
[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?
-
Feb 22nd, 2008, 01:48 PM
#2
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.
-
Feb 22nd, 2008, 03:08 PM
#3
Thread Starter
Fanatic Member
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
-
Feb 23rd, 2008, 03:55 AM
#4
Re: Check network path exists
Providing you have a network connection, that should work just fine.
-
Feb 23rd, 2008, 06:10 AM
#5
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
-
Aug 18th, 2010, 08:49 PM
#6
New Member
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
-
Aug 18th, 2010, 11:46 PM
#7
Re: Check network path exists
 Originally Posted by Hack
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|