Verify a File Path (1 Viewer)

Martin Beney

Registered User.
Local time
Today, 05:01
Joined
Mar 22, 2007
Messages
27
Hi,
Please don't laugh!

Given a string entered by a user how can I verify whether the path is a valid one. NOT that it exists or not, but is the syntax correct and idealy that the drive or UNC path exist.

I have seach the web and even asked CHATGP4 but all 'solutions' just check if it exists.

I also do not want to try and create it, trapping any errors, as its for later use when needed.

Thanks
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:31
Joined
Jan 23, 2006
Messages
15,379
How do you differentiate "valid" vs "exists"?
 

ebs17

Well-known member
Local time
Today, 01:31
Joined
Feb 7, 2020
Messages
1,946
Existing paths are always valid paths according to the syntax.

You may use your own rules for validity, for example requiring individual folders to contain or consist of certain character strings.
You would have to describe something like that in more detail.
 

Martin Beney

Registered User.
Local time
Today, 05:01
Joined
Mar 22, 2007
Messages
27
Valid is that it CAN be used to create a file, whereas Exists is that it is currently extant.

Existing is easy therefore.

What is difficult ir seems is Valid. I want the user to be able to enter ANY valid path name, with a drive or a UNC or just the default and then the rest of the path.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:31
Joined
Oct 29, 2018
Messages
21,474
Valid is that it CAN be used to create a file, whereas Exists is that it is currently extant.

Existing is easy therefore.

What is difficult ir seems is Valid. I want the user to be able to enter ANY valid path name, with a drive or a UNC or just the default and then the rest of the path.
Would you consider it valid even if the drive or unc doesn't exist yet aa long as it can be created later?
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:31
Joined
Jan 23, 2006
Messages
15,379
Perhaps you can provide examples of Valid and Not Valid. Validity seems to be something you see/understand, but which is not clear to readers.
 

Martin Beney

Registered User.
Local time
Today, 05:01
Joined
Mar 22, 2007
Messages
27
Examples? You must be kidding...

Here is some code that at least is a good start, but needs expansion...
Code:
Function IsFilePathNotValid(FilePath As String) As Byte

' formats
'   <=255 chars
'   split at \
'   each elemnt
'       not null apart than first
'       none of these chars
'           1-32  / \ " * ; < > ? |
'       and : unless first
'       and . not first or last character
'       none of these names
'           .lock, CON, PRN, AUX, NUL, COM0-COM9, LPT0-LPT9, *_vti_*, desktop.ini, any filename starting with ~$
'           forms when in the root(?)

'   Return is NOT Valid value
'       0   Valid
'       1   Too Long
'       2   Element is null
'       3   Only first element can have a :
'       4   Invalid character found
'       5   First or last character cannot be .
'       6   Illegal Name or partial name
'       7   Forms cannot be in the root
'       8   Drive has invalid format
'       9   Drive does not exist
'       10

    Dim i As Integer
    Dim j As Integer
    Dim components() As String

    ' Assume false
    IsFilePathNotValid = 0

    ' Not too long
    If Len(FilePath) > 255 Then
        IsFilePathNotValid = 1
        Exit Function
    End If

    ' Split into components (sub directories etc.)
    components = Split(FilePath, "\")

    ' Scan each element, note the first and last have some special rules
    For i = 0 To UBound(components)
        ' Only first item can be empty
        If components(i) = "" And i <> 0 Then
            IsFilePathNotValid = 2
            Exit Function
        End If

        ' Only first item can have a :
        If InStr(1, components(i), ":") > 0 And i > 0 Then
            IsFilePathNotValid = 3
            Exit Function
        End If

        ' Invalid Character present
        For j = 1 To Len(components(i))
            If Mid(components(i), j, 1) <= " " _
            Or InStr(1, "/\""*;<>?|", Mid(components(i), j, 1)) <> 0 _
            Then
                IsFilePathNotValid = 4
                Exit Function
            End If
        Next

        ' First or last character cannot be .
        If Left(components(i), 1) = "." _
        Or Right(components(i), 1) = "." _
        Then
            IsFilePathNotValid = 5
            Exit Function
        End If

        ' Invalid names
        If components(i) = ".Lock" _
        Or components(i) = "CON" _
        Or components(i) = "PRN" _
        Or components(i) = "AUX" _
        Or components(i) = "NUL" _
        Or components(i) Like "COM#" _
        Or components(i) Like "PRN#" _
        Or components(i) Like "*_vti_*" _
        Or components(i) Like "desktop.ini" _
        Or components(i) Like "-$*" _
        Then
            IsFilePathNotValid = 6
            Exit Function
        End If
    Next

    ' Special case - Forms not in root
    If InStr(1, components(0), ":") > 0 _
    And components(2) = "forms" _
    Then
        IsFilePathNotValid = 7
        Exit Function
    End If


    ' Check Drive is valid
    If InStr(1, components(0), ":") > 0 Then
        If Len(components(0)) <> 2 Then
            IsFilePathNotValid = 8
            Exit Function
        End If

        If fileSystem Is Nothing Then Set fileSystem = CreateObject("Scripting.FileSystemObject")
        If Not fileSystem.DriveExists(Left$(components(0), 1)) Then
            IsFilePathNotValid = 9
            Exit Function
        End If
    End If

    ' Further tests when figured out or needed

End Function
 

MarkK

bit cruncher
Local time
Yesterday, 16:31
Joined
Mar 17, 2004
Messages
8,181
To solve this I would just create the path. If you can create it, it is valid. If you can create it, you can delete it.
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:31
Joined
Jan 23, 2006
Messages
15,379
Code:
Function DirExists(MyDir As String) As Boolean
    DirExists = False
    If Len(Dir(MyDir, vbDirectory)) > 0 Then DirExists = True
End Function

Seems like your Valid check is some comparison with a number of local, custom strings/components.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:31
Joined
Feb 19, 2002
Messages
43,293
Valid is that it CAN be used to create a file, whereas Exists is that it is currently extant.
Search for valid folder name rules for windows. That is the standard. Here's a link that probably tells you all you need to know to validate a path.


I am not inclined to reinvent the wheel so I would try to avoid writing this code. You are not in control of the rules and if the rules change, your code needs to be changed to conform and that will be problematic going forward. That is why all suggestions so far lead to letting Windows do it.

Try giving Windows an invalid path name - what error do you get? It should be different from the "missing" error. That should be good enough for you. If you get back the "missing" error, the path does not exist but is valid. If you get back any other error, the path name is invalid. If you get no error, the path exists.
 

moke123

AWF VIP
Local time
Yesterday, 19:31
Joined
Jan 11, 2013
Messages
3,920
As far as the illegal characters in a path string, you can use the keypress event to detect those characters and throw up a message box that the character is disallowed.

Along the lines of : you'll have to look up your own ascii codes
Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)

    Select Case KeyAscii

        Case 34, 42, 59, 60, 62, 63

            KeyAscii = 0
            MsgBox "Invalid Character"

    End Select

End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom