From: Tony Toews on
Folks

I have a string which must contain just a file name, a sub folder and
file name or a complath path and file name. So what's the best means
of validating it? This file could be in the same path as the VB6
EXE.

The simplest approach would be to see if it starts with a \\ or x:
where x is A-Z. If so then it's a path and file name.

Otherwise it's a relative path and file name so insert the App.Path in
front.

Am I missing something?

Tony
From: Thorsten Albers on
Tony Toews <ttoews(a)telusplanet.net> schrieb im Beitrag
<dhc516tqmbea168re9b6u1erue8m8js9c5(a)4ax.com>...
> I have a string which must contain just a file name, a sub folder and
> file name or a complath path and file name. So what's the best means
> of validating it? This file could be in the same path as the VB6
> EXE.
>
> The simplest approach would be to see if it starts with a \\ or x:
> where x is A-Z. If so then it's a path and file name.
>
> Otherwise it's a relative path and file name so insert the App.Path in
> front.
>
> Am I missing something?

Another possibility is a drive/server+share specification with a file name
without a diretcory path, e.g. C:autoexec.bat. In this case you have to
remove the drive specification from App.Path and insert the remaining
directory path between drive/server+share specification.

But why App.Path? If a relative path is given, usually it is not relative
to the application directory but to the current directory of the
application (curdir$).

My approach to this would be as follows:
1. Check, if the given relative or absolute path points to an existing file
(GetFileAttributes() etc.)
2. Get the absolute file path by calling GetFullPathName() of the Windows
API.

Step 1 is necessary since on some version of windows GetFullPathName()
sometimes returns curious results if the file does not exist....

Other usefull procedures for validating, etc. a path can be found in
SHLWAPI.DLL (e.g. PathIsRelative()).

--
Thorsten Albers

albers (a) uni-freiburg.de

From: Karl E. Peterson on
Tony Toews wrote:
> Folks
>
> I have a string which must contain just a file name, a sub folder and
> file name or a complath path and file name. So what's the best means
> of validating it? This file could be in the same path as the VB6
> EXE.
>
> The simplest approach would be to see if it starts with a \\ or x:
> where x is A-Z. If so then it's a path and file name.
>
> Otherwise it's a relative path and file name so insert the App.Path in
> front.
>
> Am I missing something?

To be sure I understand, you want to be sure it's a "valid" filename?
Not necessarily that it exists?

This function will tell you whether the path is relative or absolute,
if that's what you're wanting:

Private Declare Function PathIsRelativeW Lib "shlwapi" (ByVal
lpszPath As Long) As Boolean

Public Function PathIsRelative(ByVal Path As String) As Boolean
' Searches a path and determines if it is relative.
PathIsRelative = PathIsRelativeW(StrPtr(Path))
End Function

And this is my favorite way to build paths, now:

Private Declare Function PathCombineW Lib "shlwapi" (ByVal lpszDest
As Long, ByVal lpszDir As Long, ByVal lpszFile As Long) As Boolean

Public Function PathCombine(ByVal Directory As String, ByVal File As
String) As String
Dim Buffer As String
' Concatenates two strings that represent properly formed
' paths into one path, as well as any relative path pieces.
Buffer = String$(MAX_PATH, 0)
If PathCombineW(StrPtr(Buffer), StrPtr(Directory), StrPtr(File))
Then
PathCombine = TrimNull(Buffer)
End If
End Function

PathCombine means you don't have to worry about backslashes anymore.

(Requires Win98/2000 or IE4 or higher.)

--
..NET: It's About Trust! http://vfred.mvps.org
Customer Hatred Knows No Bounds at MSFT
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org


From: Karl E. Peterson on
Tony Toews wrote:
> Folks
>
> I have a string which must contain just a file name, a sub folder and
> file name or a complath path and file name. So what's the best means
> of validating it? This file could be in the same path as the VB6
> EXE.
>
> The simplest approach would be to see if it starts with a \\ or x:
> where x is A-Z. If so then it's a path and file name.
>
> Otherwise it's a relative path and file name so insert the App.Path in
> front.
>
> Am I missing something?

It occurs to me you may want to do more than just realize whether the
path is relative or not. Here's a function that will fully qualify a
relative path based on the working directory:

Private Declare Function PathSearchAndQualifyW Lib "shlwapi" (ByVal
lpszPath As Long, ByVal lpszFullyQualifiedPath As Long, ByVal
cchFullyQualifiedPath As Long) As Boolean

Public Function PathQualify(ByVal Path As String) As String
Dim Buffer As String
' Returns True if the path is correctly formatted and
' fully qualified. If the path name doesn't contain
' folder info, the name of the active directory is used
' to create a qualified path.
Buffer = String$(MAX_PATH, 0)
If PathSearchAndQualifyW(StrPtr(Path), StrPtr(Buffer), MAX_PATH)
Then
PathQualify = TrimNull(Buffer)
End If
End Function

I realize that may not be what you want, because you seem to have an
anchor at App.Path, but just in case?

--
..NET: It's About Trust! http://vfred.mvps.org
Customer Hatred Knows No Bounds at MSFT
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org


From: Tony Toews on
On Fri, 11 Jun 2010 15:36:00 -0700, Karl E. Peterson <karl(a)exmvps.org>
wrote:

>To be sure I understand, you want to be sure it's a "valid" filename?
>Not necessarily that it exists?

Yes, the file must exist.

>(Requires Win98/2000 or IE4 or higher.)

Yup, that's fine. Although a few years ago I did get a report of a
bug in A2.0 which dates from 1994 and a few years before that a bug
with NT 4.0.

Tony