How to check a string to see that it is a valid file name

  • Thread starter Thread starter Academia
  • Start date Start date
A

Academia

I'd like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path


An help for either of the above would be appreciated.
 
I'd like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.

If you give the FileInfo class the full path to the file it can
determine if the file exists.

dim fi as new System.IO.FileInfo("[Path]")
if fi.Exists then
'do work
end if
 
I'd like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.

Well... That depends on what your trying to accomplish? If your
checking for existance, then there is always System.IO.File.Exists.
If your just checking to make sure that you have a potentially valid
file name, then you can use the System.IO.Path classes
GetInvalidFileNameChars and GetInvalidPathChars to get an array of
characters that are not allowed in a file name and path, then you
could check to make sure your file name doesn't contain any of those
characters, using the String.IndexOfAny. If it returns anything other
then negative 1, then you have a bad filename:

dim bfc() as char = path.getinvalidfilenamechars()

if filename.indexofany(bfc) > -1 then
' illegal file name
end if
 
Thanks but I know the file does not exist (yet).

Thanks for answering


cfps.Christian said:
I'd like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.

If you give the FileInfo class the full path to the file it can
determine if the file exists.

dim fi as new System.IO.FileInfo("[Path]")
if fi.Exists then
'do work
end if
 
I had read the help for GetInvalidPathChars and was confused about what its
warning meant.
I had also checked and the characters are only "<>|
If I include a * in the filename an SaveAs file dialog box will not return
if I click OK.
Not sure if that means * is NG or if it's is just the dialogbox's treatment.
I tried / and Notebook's file save dialogbox says it is invalid.

So I'm confused about what to believe.


Thanks
 
Also GetInvalidFilenameChars does not include \ and :
Seems it should. Are they OK in a filename as apposed to a path?

thanks


Academia said:
I had read the help for GetInvalidPathChars and was confused about what its
warning meant.
I had also checked and the characters are only "<>|
If I include a * in the filename a SaveAs file dialog box will not return
if I click OK.
Not sure if that means * is NG or if it's is just the dialogbox's
treatment.
I tried / and Notebook's file save dialogbox says it is invalid.

So I'm confused about what to believe.


Thanks
 
I always wondered about MAX_PATH.
Thinking there might have to be room for a Null at the end (or maybe 2
nulls)


Is name.Length > MAX_PATH then
errorCode
end if

correct?
 
Back
Top