From: The Frog on
Hi Everyone,

I was just rummaging around and came across a startling fact (for me
anyway). I have no routine to parse a windows or unc path and filename
to see if it is well formed or not. I had a bit if a dig around on teh
net and mainly found debtes on what is valid and what isnt, complete
with loads of non-functional regular expressions.

I have two questions:
1/ Is it that hard to actually parse a windows file / path or unc path
for 'well formedness'
2/ Is there an accepted method of doing this parsing?

Can anyone point me in the right direction here? It is not an urgent
thing but something I will need to take care of in the next week or
two. If anybody has any data on this or sample code I would greatly
appreciate the pointers (or even a complete module!)

Cheers

The Frog
From: Stuart McCall on
"The Frog" <mr.frog.to.you(a)googlemail.com> wrote in message
news:b7b19001-5e51-490b-a4c8-38f43969f901(a)35g2000yqm.googlegroups.com...
> Hi Everyone,
>
> I was just rummaging around and came across a startling fact (for me
> anyway). I have no routine to parse a windows or unc path and filename
> to see if it is well formed or not. I had a bit if a dig around on teh
> net and mainly found debtes on what is valid and what isnt, complete
> with loads of non-functional regular expressions.
>
> I have two questions:
> 1/ Is it that hard to actually parse a windows file / path or unc path
> for 'well formedness'
> 2/ Is there an accepted method of doing this parsing?
>
> Can anyone point me in the right direction here? It is not an urgent
> thing but something I will need to take care of in the next week or
> two. If anybody has any data on this or sample code I would greatly
> appreciate the pointers (or even a complete module!)
>
> Cheers
>
> The Frog

Here's what I use. It covers all of the mistakes in paths that I've ever
seen:

Public Function IsLegalFileName(ByVal s As String, Optional FullPath As
Boolean) As Boolean
Const Illegals = "<>\/:?|*" & """"
Dim sl As Long, i As Long
'
sl = Len(s)
If sl = 0 Or sl > 260 Then Exit Function
If FullPath Then
Select Case Mid$(s, 2, 1)
Case ":", "\"
'do nothing
Case Else
Exit Function
End Select
End If
For i = 1 To sl
If InStr(1, Illegals, Mid$(s, i, 1)) Then
Exit Function
End If
Next
IsLegalFileName = True
End Function

I'm sure someone could make the task far more complicated, but as I say this
covers everything I've come across (for years). Hope it helps.


From: Stuart McCall on
"Stuart McCall" <smccall(a)myunrealbox.com> wrote in message
news:WLmqn.71$xw.55(a)newsfe19.ams2...
> "The Frog" <mr.frog.to.you(a)googlemail.com> wrote in message
> news:b7b19001-5e51-490b-a4c8-38f43969f901(a)35g2000yqm.googlegroups.com...
>> Hi Everyone,
>>
>> I was just rummaging around and came across a startling fact (for me
>> anyway). I have no routine to parse a windows or unc path and filename
>> to see if it is well formed or not. I had a bit if a dig around on teh
>> net and mainly found debtes on what is valid and what isnt, complete
>> with loads of non-functional regular expressions.
>>
>> I have two questions:
>> 1/ Is it that hard to actually parse a windows file / path or unc path
>> for 'well formedness'
>> 2/ Is there an accepted method of doing this parsing?
>>
>> Can anyone point me in the right direction here? It is not an urgent
>> thing but something I will need to take care of in the next week or
>> two. If anybody has any data on this or sample code I would greatly
>> appreciate the pointers (or even a complete module!)
>>
>> Cheers
>>
>> The Frog
>
> Here's what I use. It covers all of the mistakes in paths that I've ever
> seen:
>
> Public Function IsLegalFileName(ByVal s As String, Optional FullPath As
> Boolean) As Boolean
> Const Illegals = "<>\/:?|*" & """"
> Dim sl As Long, i As Long
> '
> sl = Len(s)
> If sl = 0 Or sl > 260 Then Exit Function
> If FullPath Then
> Select Case Mid$(s, 2, 1)
> Case ":", "\"
> 'do nothing
> Case Else
> Exit Function
> End Select
> End If
> For i = 1 To sl
> If InStr(1, Illegals, Mid$(s, i, 1)) Then
> Exit Function
> End If
> Next
> IsLegalFileName = True
> End Function
>
> I'm sure someone could make the task far more complicated, but as I say
> this covers everything I've come across (for years). Hope it helps.

I just realised I should have explained the purpose of the FullPath
parameter. If you leave the option out, or pass False, the routine won't
check for <drive letter> followed by colon, or "\\" in the case of UNC
paths. Pass True and these checks are made, both on the line:

Case ":", "\"


From: Krzysztof Naworyta on
Juzer The Frog <mr.frog.to.you(a)googlemail.com> napisa�
| Hi Everyone,
|
| I was just rummaging around and came across a startling fact (for me
| anyway). I have no routine to parse a windows or unc path and filename
| to see if it is well formed or not. I had a bit if a dig around on teh
| net and mainly found debtes on what is valid and what isnt, complete
| with loads of non-functional regular expressions.
|
| I have two questions:
| 1/ Is it that hard to actually parse a windows file / path or unc path
| for 'well formedness'
| 2/ Is there an accepted method of doing this parsing?
|
| Can anyone point me in the right direction here? It is not an urgent
| thing but something I will need to take care of in the next week or
| two. If anybody has any data on this or sample code I would greatly
| appreciate the pointers (or even a complete module!)


You can use very usefull dll: shlwapi


Here declarations of many functions, related to paths:

Private Declare Function apiPathCombine _
Lib "shlwapi.dll" Alias "PathCombineA" _
( _
ByVal szDest As String, _
ByVal lpszDir As String, _
ByVal lpszFile As String _
) As Long

Private Declare Function apiPathCommonPrefix _
Lib "shlwapi.dll" Alias "PathCommonPrefixA" _
( _
ByVal pszFile1 As String, _
ByVal pszFile2 As String, _
ByVal achPath As String _
) As Long

Private Declare Function apiPathCompactPath _
Lib "shlwapi.dll" Alias "PathCompactPathA" _
( _
ByVal hDC As Long, _
ByVal pszPath As String, _
ByVal dx As Long _
) As Long

Private Declare Function apiPathCompactPathEx _
Lib "shlwapi.dll" Alias "PathCompactPathExA" _
( _
ByVal pszOut As String, _
ByVal pszSrc As String, _
ByVal cchMax As Long, _
ByVal dwFlags As Long _
) As Long

Private Declare Sub apiPathCreateFromUrl _
Lib "shlwapi.dll" Alias "PathCreateFromUrlA" _
( _
ByVal pszUrl As String, _
ByVal pszPath As String, _
ByRef pcchPath As Long, _
ByVal dwFlags As Long _
)

Private Declare Function apiPathAddBackslash _
Lib "shlwapi.dll" Alias "PathAddBackslashA" _
( _
ByVal pszPath As String _
) As Long

Private Declare Function apiPathAddExtension _
Lib "shlwapi.dll" Alias "PathAddExtensionA" _
( _
ByVal pszPath As String, _
ByVal pszExt As String _
) As Long

Private Declare Function apiPathAppend _
Lib "shlwapi.dll" Alias "PathAppendA" _
( _
ByVal pszPath As String, _
ByVal pMore As String _
) As Long

Private Declare Function apiPathBuildRoot _
Lib "shlwapi.dll" Alias "PathBuildRootA" _
( _
ByVal szRoot As String, _
ByVal iDrive As Long _
) As Long

Private Declare Function apiPathCanonicalize _
Lib "shlwapi.dll" Alias "PathCanonicalizeA" _
( _
ByVal pszBuf As String, _
ByVal pszPath As String _
) As Long
'

Private Declare Function apiPathIsDirectory Lib "shlwapi.dll" _
Alias "PathIsDirectoryA" _
( _
ByVal pszPath As String _
) As Long

Private Declare Function apiPathIsDirectoryEmpty Lib "shlwapi.dll" _
Alias "PathIsDirectoryEmptyA" _
( _
ByVal pszPath As String _
) As Long

Private Declare Function apiPathIsLFNFileSpec Lib "shlwapi.dll" _
Alias "PathIsLFNFileSpecA" _
( _
ByVal lpName As String _
) As Long

Private Declare Function apiPathIsNetworkPath Lib "shlwapi.dll" _
Alias "PathIsNetworkPathA" _
( _
ByVal pszPath As String _
) As Long

Private Declare Function apiPathIsPrefix Lib "shlwapi.dll" _
Alias "PathIsPrefixA" _
( _
ByVal pszPrefix As String _
, ByVal pszPath As String _
) As Long

Private Declare Function apiPathIsRelative Lib "shlwapi.dll" _
Alias "PathIsRelativeA" _
( _
ByVal pszPath As String _
) As Long

Private Declare Function apiPathIsRoot Lib "shlwapi.dll" _
Alias "PathIsRootA" _
( _
ByVal pszPath As String _
) As Long






--
KN

From: The Frog on
Thanks to both of you for your knowledge sharing. I deeply appreciate
it. I was growing increasingly concerned that I would need to create
some 'super parser' to be able to deal with the issue having not
realised how convoluted the problem really is. These are both simple
and direct tools I can use. Thankyou

Cheers

The Frog