Home All Groups Group Topic Archive Search About

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

Author
21 Nov 2007 8:01 PM
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.

Author
21 Nov 2007 8:12 PM
cfps.Christian
On Nov 21, 2:01 pm, "Academia" <academiaNOS***@a-znet.com> wrote:
> 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
Author
21 Nov 2007 8:45 PM
Academia
Thanks but I know the file does not exist (yet).

Thanks for answering


Show quoteHide quote
"cfps.Christian" <ge0193***@otc.edu> wrote in message
news:e5fee764-7be5-47f8-9b25-8cfe26d5c76e@w28g2000hsf.googlegroups.com...
> On Nov 21, 2:01 pm, "Academia" <academiaNOS***@a-znet.com> wrote:
>> 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
Author
21 Nov 2007 8:20 PM
Tom Shelton
On Nov 21, 1:01 pm, "Academia" <academiaNOS***@a-znet.com> wrote:
> 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

--
Tom Shelton
Author
21 Nov 2007 8:54 PM
Academia
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



Show quoteHide quote
"Tom Shelton" <tom_shel***@comcast.net> wrote in message
news:f0a8b1cf-630a-4ac4-b60e-959fc5c4d7da@f13g2000hsa.googlegroups.com...
> On Nov 21, 1:01 pm, "Academia" <academiaNOS***@a-znet.com> wrote:
>> 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
>
> --
> Tom Shelton
>
Author
21 Nov 2007 9:10 PM
Academia
Also GetInvalidFilenameChars does not include \ and :
Seems it should. Are they OK in a filename as apposed to a path?

thanks


Show quoteHide quote
"Academia" <academiaNOSPAM@a-znet.com> wrote in message
news:ufDjBDILIHA.2176@TK2MSFTNGP06.phx.gbl...
>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
>
>
>
> "Tom Shelton" <tom_shel***@comcast.net> wrote in message
> news:f0a8b1cf-630a-4ac4-b60e-959fc5c4d7da@f13g2000hsa.googlegroups.com...
>> On Nov 21, 1:01 pm, "Academia" <academiaNOS***@a-znet.com> wrote:
>>> 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
>>
>> --
>> Tom Shelton
>>
>
>
Author
22 Nov 2007 9:37 AM
Andrew Morton
Academia wrote:
> Also GetInvalidFilenameChars does not include \ and :
> Seems it should. Are they OK in a filename as apposed to a path?

There are characters which are valid for NTFS but may not be valid for the
OS.

In particular, note the last paragraph of
http://blogs.msdn.com/brian_dewey/archive/2004/01/19/60263.aspx

Andrew
Author
22 Nov 2007 5:38 PM
Academia
Good site

thanks

Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:uWiKv3OLIHA.5580@TK2MSFTNGP02.phx.gbl...
> Academia wrote:
>> Also GetInvalidFilenameChars does not include \ and :
>> Seems it should. Are they OK in a filename as apposed to a path?
>
> There are characters which are valid for NTFS but may not be valid for the
> OS.
>
> In particular, note the last paragraph of
> http://blogs.msdn.com/brian_dewey/archive/2004/01/19/60263.aspx
>
> Andrew
>
Author
22 Nov 2007 2:58 AM
Eternal Snow
try to create a file with that name/path, then delete it.... XD

Show quoteHide quote
"Academia" <academiaNOSPAM@a-znet.com> wrote in message
news:%23hI9%23kHLIHA.1212@TK2MSFTNGP05.phx.gbl...
> 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.
>
>
>
>
>
>
>
Author
22 Nov 2007 5:39 PM
Academia
I don't know much about memory files but would that be a way to go?

Thanks

Show quoteHide quote
"Eternal Snow" <allen_st_cl***@msn.com> wrote in message
news:9D6F86F6-F769-4EBA-A562-4E285D7D7098@microsoft.com...
> try to create a file with that name/path, then delete it.... XD
>
> "Academia" <academiaNOSPAM@a-znet.com> wrote in message
> news:%23hI9%23kHLIHA.1212@TK2MSFTNGP05.phx.gbl...
>> 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.
>>
>>
>>
>>
>>
>>
>>
>
Author
22 Nov 2007 5:52 PM
Academia
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?


Show quoteHide quote
"Academia" <academiaNOSPAM@a-znet.com> wrote in message
news:%23hI9%23kHLIHA.1212@TK2MSFTNGP05.phx.gbl...
> 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.
>
>
>
>
>
>
>