I need a test to see that a string is a valid path to a file

A

AAaron123

I know this isn't a regular expression NG but there does not appear to be
one in the microsoft series which is all my news reader shows.

I need a test to see that a string is a valid path to a file.

The string does not have a drivename nor a filename, simply a collection of
folder names, but I could add that for the test.

I don't know anything about regular expressions so I looked on the Internet
and found some.

The following two are quite different in length.

I'm hoping someone with a little experience can help me select one or maybe
share the one they use.

One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?

Thanks in advance
//Matches filenames

//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$



//Matches drives, folders and file on a Windows OS. Folder matches must end
with \ Folder and file names can not end with a space. I limited the file
extension length to 15 though that not a restriction of a file's extension
This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005

//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?![.\x20](?:\\|$))[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
..]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$
 
F

Family Tree Mike

AAaron123 said:
I know this isn't a regular expression NG but there does not appear to be
one in the microsoft series which is all my news reader shows.

I need a test to see that a string is a valid path to a file.

The string does not have a drivename nor a filename, simply a collection
of folder names, but I could add that for the test.

I don't know anything about regular expressions so I looked on the
Internet and found some.

The following two are quite different in length.

I'm hoping someone with a little experience can help me select one or
maybe share the one they use.

One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?

Thanks in advance
//Matches filenames

//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$



//Matches drives, folders and file on a Windows OS. Folder matches must
end with \ Folder and file names can not end with a space. I limited the
file extension length to 15 though that not a restriction of a file's
extension This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005

//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?![.\x20](?:\\|$))[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
.]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$


I can't help with regex, but this seems easier to me:

bool ValidateFilename(string FN)
{
try
{
string fn = Path.GetFileName(FN);
string dir = Path.GetFullPath(FN);

bool ok = (fn != string.Empty) && (dir != string.Empty);
if (ok)
{
ok = (fn.IndexOfAny(Path.GetInvalidFileNameChars(), 0)
== -1);
if (ok) ok = (dir.IndexOfAny(Path.GetInvalidPathChars(),
0) == -1);
}
return ok;
}
catch { return false; }
}
 
H

Hans Kesting

AAaron123 explained :
I know this isn't a regular expression NG but there does not appear to be one
in the microsoft series which is all my news reader shows.

I need a test to see that a string is a valid path to a file.

What do you mean?
"does point to an existing file": you can't check that with a regex.
"seems like it could be a valid file": that is possible
The string does not have a drivename nor a filename, simply a collection of
folder names, but I could add that for the test.

I don't know anything about regular expressions so I looked on the Internet
and found some.

The following two are quite different in length.

I'm hoping someone with a little experience can help me select one or maybe
share the one they use.

One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?

There are various flavours or dialects.
For instance javascript (in a browser) understands just a subset of
what is available to .Net languages (javascript would not understand
your examples). But I think most of the features are pretty
standardized.
Thanks in advance
//Matches filenames

//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$

Accepts just a 2 to 6 letter extension ("file.c" would fail, as would
"something.a1").
Also it needs to start with a drive letter, which you didn't want/need.
//Matches drives, folders and file on a Windows OS. Folder matches must end
with \ Folder and file names can not end with a space. I limited the file
extension length to 15 though that not a restriction of a file's extension
This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005

//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?![.\x20](?:\\|$))[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
.]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$

This required a 2-15 character extension and maybe supports too many
characters for directory- and filenames (everything from hexcode 20 up
to hexcode 7E, which does include illegal characters).


The code that Mike gave looks OK. Or do you really need a regex?

Hans Kesting
 
A

AAaron123

Family Tree Mike said:
AAaron123 said:
I know this isn't a regular expression NG but there does not appear to be
one in the microsoft series which is all my news reader shows.

I need a test to see that a string is a valid path to a file.

The string does not have a drivename nor a filename, simply a collection
of folder names, but I could add that for the test.

I don't know anything about regular expressions so I looked on the
Internet and found some.

The following two are quite different in length.

I'm hoping someone with a little experience can help me select one or
maybe share the one they use.

One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?

Thanks in advance
//Matches filenames

//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$



//Matches drives, folders and file on a Windows OS. Folder matches must
end with \ Folder and file names can not end with a space. I limited the
file extension length to 15 though that not a restriction of a file's
extension This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005

//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?![.\x20](?:\\|$))[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
.]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$


I can't help with regex, but this seems easier to me:

bool ValidateFilename(string FN)
{
try
{
string fn = Path.GetFileName(FN);
string dir = Path.GetFullPath(FN);

bool ok = (fn != string.Empty) && (dir != string.Empty);
if (ok)
{
ok = (fn.IndexOfAny(Path.GetInvalidFileNameChars(), 0)
== -1);
if (ok) ok =
(dir.IndexOfAny(Path.GetInvalidPathChars(), 0) == -1);
}
return ok;
}
catch { return false; }
}

You got me looking at Path as an approach so I read about it and noticed in
the Path Class description:

Path members do, however, validate the contents of a specified path string,
and throw an ArgumentException if the string contains characters that are
not valid in path strings, as defined in InvalidPathChars.

So I tested it and it seems that it checks not only the path string but also
the file name string so that the following might be all that is needed.

public static bool ValidateFilename(string FN)

{

if (FN.IndexOf("\\\\") != -1)

{

return false; //Two backslashs in string

}

try

{

string fn = Path.GetFileName(FN);

return true;

}

catch

{

return false;

}

}
I assume \\ is not legal and had to implicitly check because it was not
picked up by GetFileName. I have no idea if there are other configurations
of legal characters that might not be acceptable and not picked up by
GetFileName!

Also, if the last substring is really a folder and is not followed by a \ it
would be checked as a filename (I guess) which may not be correct.

Also, the usage of forward slash has me confused. In ASP.NET at least, it
appears that forward slash can be used in place of back slashs - but I'm
not sure about that.

I'd sure like to see comments about the above!


Thanks a lot for pointing to Path as an approach
 
A

AAaron123

Hans Kesting said:
AAaron123 explained :
I know this isn't a regular expression NG but there does not appear to be
one in the microsoft series which is all my news reader shows.

I need a test to see that a string is a valid path to a file.

What do you mean?
"does point to an existing file": you can't check that with a regex.
"seems like it could be a valid file": that is possible
The string does not have a drivename nor a filename, simply a collection
of folder names, but I could add that for the test.

I don't know anything about regular expressions so I looked on the
Internet and found some.

The following two are quite different in length.

I'm hoping someone with a little experience can help me select one or
maybe share the one they use.

One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?

There are various flavours or dialects.
For instance javascript (in a browser) understands just a subset of what
is available to .Net languages (javascript would not understand your
examples). But I think most of the features are pretty standardized.
Thanks in advance
//Matches filenames

//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$

Accepts just a 2 to 6 letter extension ("file.c" would fail, as would
"something.a1").
Also it needs to start with a drive letter, which you didn't want/need.
//Matches drives, folders and file on a Windows OS. Folder matches must
end with \ Folder and file names can not end with a space. I limited the
file extension length to 15 though that not a restriction of a file's
extension This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005

//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?![.\x20](?:\\|$))[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
.]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$

This required a 2-15 character extension and maybe supports too many
characters for directory- and filenames (everything from hexcode 20 up to
hexcode 7E, which does include illegal characters).
That's obvious immediately after looking at the expression above. Don't know
how I missed that.

Just kidding!

No, I'm not fixed to regex simply thought of that as an approach. But Mike's
appears better. My reply to Mike shows I still have issues; maybe you can
help again by commenting on one of them. I'd sure apppreciate it.

Thanks a lot
 
F

Family Tree Mike

AAaron123 said:
You got me looking at Path as an approach so I read about it and noticed
in the Path Class description:

Path members do, however, validate the contents of a specified path
string, and throw an ArgumentException if the string contains characters
that are not valid in path strings, as defined in InvalidPathChars.

So I tested it and it seems that it checks not only the path string but
also the file name string so that the following might be all that is
needed.

public static bool ValidateFilename(string FN)

{

if (FN.IndexOf("\\\\") != -1)

{

return false; //Two backslashs in string

}

try

{

string fn = Path.GetFileName(FN);

return true;

}

catch

{

return false;

}

}
I assume \\ is not legal and had to implicitly check because it was not
picked up by GetFileName. I have no idea if there are other configurations
of legal characters that might not be acceptable and not picked up by
GetFileName!

Also, if the last substring is really a folder and is not followed by a \
it would be checked as a filename (I guess) which may not be correct.

Also, the usage of forward slash has me confused. In ASP.NET at least, it
appears that forward slash can be used in place of back slashs - but I'm
not sure about that.

I'd sure like to see comments about the above!


Thanks a lot for pointing to Path as an approach


Well, \\ at the start of the test string should be perfectly valid for a
network share. I think that most cases where double slashes occur internal
to the string, the system does treat it as a single slash. Otherwise I
think your code is fine. I was simply trying to provide info that the
GetInvalidFileNameChars and GetInvalidPathChars functions are there to use
as well.
 
A

AAaron123

Family Tree Mike said:
Well, \\ at the start of the test string should be perfectly valid for a
network share. I think that most cases where double slashes occur
internal to the string, the system does treat it as a single slash.
Otherwise I think your code is fine. I was simply trying to provide info
that the GetInvalidFileNameChars and GetInvalidPathChars functions are
there to use as well.

Thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top