dbTalk Databases Forums  

Parsing well formed paths and filenames

comp.databases.ms-access comp.databases.ms-access


Discuss Parsing well formed paths and filenames in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Frog
 
Posts: n/a

Default Parsing well formed paths and filenames - 03-24-2010 , 02:22 AM






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

Reply With Quote
  #2  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-24-2010 , 05:33 AM






"The Frog" <mr.frog.to.you (AT) googlemail (DOT) com> wrote

Quote:
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.

Reply With Quote
  #3  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-24-2010 , 05:46 AM



"Stuart McCall" <smccall (AT) myunrealbox (DOT) com> wrote

Quote:
"The Frog" <mr.frog.to.you (AT) googlemail (DOT) com> wrote in message
news:b7b19001-5e51-490b-a4c8-38f43969f901 (AT) 35g2000yqm (DOT) 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 ":", "\"

Reply With Quote
  #4  
Old   
Krzysztof Naworyta
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-24-2010 , 06:29 AM



Juzer The Frog <mr.frog.to.you (AT) googlemail (DOT) com> napisał
Quote:
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

Reply With Quote
  #5  
Old   
The Frog
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-25-2010 , 01:33 AM



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

Reply With Quote
  #6  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-25-2010 , 06:25 AM



"The Frog" <mr.frog.to.you (AT) googlemail (DOT) com> wrote

Quote:
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
Just one more piece of knowledge I think will be useful for you is this:
You're probably concerned with users typing in badly formed paths, right? If
so then you ought to present them with the windows open/save dialog as this
will handle the entire issue for you. The dialog will not return a badly
formed path. Plus users can choose to navigate via the folder list or type
the path themselves. Find code to implement the dialog here:

http://www.smccall.demon.co.uk/MiscApi.htm#FileOpen

HTH

Reply With Quote
  #7  
Old   
The Frog
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-25-2010 , 08:23 AM



Stuart,

Once again thankyou. In my app that I am building I am using this
approach already. The problem 'may' come when one of the clases I have
built is used by another person without following this approach. I
wanted to control for errors creeping into the class and handle them
gracefully (raise an event stating path/file is rubbish so go do it
again :-) The code you posted above has allowed me to solve this for
the largest part. I think going beyond what you have posted is
probably not worth the effort - after all I do have documentation with
the class as well.

Thanks once again.

The Frog

Reply With Quote
  #8  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-25-2010 , 12:57 PM



Quote:
Thanks once again.

The Frog
You're welcome.

Reply With Quote
  #9  
Old   
David W. Fenton
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-25-2010 , 02:21 PM



"Krzysztof Naworyta" <k.naworyta (AT) datacomp (DOT) com.pl> wrote in
news:hod0ir$ivo$1 (AT) news (DOT) onet.pl:

Quote:
You can use very usefull dll: shlwapi
The File System Object is a useful wrapper around all of these
functions, no?

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Reply With Quote
  #10  
Old   
The Frog
 
Posts: n/a

Default Re: Parsing well formed paths and filenames - 03-26-2010 , 01:35 AM



Hi David,

I use FSO for achieving my file / folder selection. And I agree that
it is a nice wrapper. I was also interesting to play with the API
directly - a little more complex but produces essentially the same
result. I am using several classes that work together to achieve a
goal, although some of them can work without the others, hence my
issue above. I am comfortable that this is now controlled for in my
application and that if the class in question is used separately by
another person there are reasonable safeguards against crashing and
erroneous use.

The Frog

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.