Check path for errors

BamaColtsFan

Registered User.
Local time
Today, 15:54
Joined
Nov 8, 2006
Messages
91
Hey gang....

Next topic - Error Handling!

In the code below I'm trying to evaluate the value of strPath to determine if the file specified exists or not. If it doesn't, I want a message box to come up and the function to exit. Obviously, I'm not doing it right but I really have no clue how to get VB to look down the path to decide if the file is good or not. I'm pretty sure everything else will work if I can figure out how to get a true or false value for the "IF" statement...


Code:
If strPath IsError Then
MsgBox "The file " & strFileName & " does not exist in the path specified." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "Please correct and try again!"
Exit Function
End If

As always, the guidance and wisdom found herein is deeply appreciated!
 
Try
Code:
If Dir(strPath)="" Then
MsgBox "The file " & strFileName & " does not exist in the path specified." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "Please correct and try again!"
Exit Function
End If
 
Hey gang....

Next topic - Error Handling!

In the code below I'm trying to evaluate the value of strPath to determine if the file specified exists or not. If it doesn't, I want a message box to come up and the function to exit. Obviously, I'm not doing it right but I really have no clue how to get VB to look down the path to decide if the file is good or not. I'm pretty sure everything else will work if I can figure out how to get a true or false value for the "IF" statement...


Code:
If strPath IsError Then
MsgBox "The file " & strFileName & " does not exist in the path specified." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "Please correct and try again!"
Exit Function
End If

As always, the guidance and wisdom found herein is deeply appreciated!


You have to use the file system object. Here is a basic set that I use to look for files that have the same names but different dates. If there is a file with a newer date than one I have linked in it issues an error.

Code:
Private Function isNewFile(ByRef curDate As Date, ByRef strCurDt As String) As Boolean
Dim fso
Dim fls
Dim path1 As String
Dim file1 As String
Dim strTempDt As String
Dim tempDt As Date
path1 = "C:\Sales Reporting\Reporting 2009\Weekly\Monday\Small Bus Performance"
file1 = "BACC Offers_"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fls = fso.GetFolder(path1).files
For Each f In fls
    If f.Name Like "BACC Offers_*" Then
        strTempDt = Mid(f.Name, 13, 6)
        strTempDt = Mid(strTempDt, 1, 2) & "/" _
            & Mid(strTempDt, 3, 2) & "/" _
            & Mid(strTempDt, 5, 2)
        tempDt = Format(strTempDt, "mm/dd/yyyy")
        
        If tempDt > curDate Then
            curDate = tempDt
            strCurDt = Mid(f.Name, 13, 6)
            isNewFile = True
        End If
    End If
Next
Set fso = Nothing
Set fls = Nothing
End Function

The more simplified version is a fso.FileExists or fso.FolderExists. Each of these returns a boolean value.
 
You can do this using the FileSearch object, but you will need the path to the folder without the file name on the end for this:
Code:
With Application.FileSearch
    .NewSearch
    .LookIn = strFolderPath
    .SearchSubFolders = False
    .FileName = strFileName
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles
 
    If .Execute = 0 Then '.Execute equals zero if file not find, otherwise >0
        MsgBox "The file " & strFileName & " does not exist in the path specified." & vbNewLine & vbNewLine & _
        "Quitting...", vbCritical, "Please correct and try again!"
        Exit Function
    End If
End With
 
The built in Access Dir function is much simpler and does the same thing!!

No need to write your own function!
 
Excellent! Thanks Ray... I don't think I would have thought to use the Dir function!
 
Nice code Ray! Not a function I was really aware of (hence my longer code), but I'm sure I'll have many uses for it now! Thanks.

(In case you're wondering, when I started typing my earlier reply there'd been no other replies, which I think is why we have 3 very different answers in a row).
 
dir is useful, but will only retrieve file names, not sizes etc.
oyu can use some attributes to filter the files.

dir is not recursive unfortunately, so you cant check through folders and subfolders.
 

Users who are viewing this thread

Back
Top Bottom