By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.



Nov 21 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Nov 21, 2:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:
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
Nov 21 '07 #2

P: n/a
On Nov 21, 1:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:
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

Nov 21 '07 #3

P: n/a
Thanks but I know the file does not exist (yet).

Thanks for answering
"cfps.Christian" <ge*******@otc.eduwrote in message
news:e5**********************************@w28g2000 hsf.googlegroups.com...
On Nov 21, 2:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:
>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

Nov 21 '07 #4

P: n/a
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

"Tom Shelton" <to*********@comcast.netwrote in message
news:f0**********************************@f13g2000 hsa.googlegroups.com...
On Nov 21, 1:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:
>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

Nov 21 '07 #5

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

thanks
"Academia" <ac************@a-znet.comwrote in message
news:uf**************@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" <to*********@comcast.netwrote in message
news:f0**********************************@f13g2000 hsa.googlegroups.com...
>On Nov 21, 1:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:
>>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


Nov 21 '07 #6

P: n/a
try to create a file with that name/path, then delete it.... XD

"Academia" <ac************@a-znet.comwrote in message
news:%2******************@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.


Nov 22 '07 #7

P: n/a
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/ar.../19/60263.aspx

Andrew
Nov 22 '07 #8

P: n/a
Good site

thanks

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:uW**************@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/ar.../19/60263.aspx

Andrew

Nov 22 '07 #9

P: n/a
I don't know much about memory files but would that be a way to go?

Thanks

"Eternal Snow" <al************@msn.comwrote in message
news:9D**********************************@microsof t.com...
try to create a file with that name/path, then delete it.... XD

"Academia" <ac************@a-znet.comwrote in message
news:%2******************@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.



Nov 22 '07 #10

P: n/a
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?
"Academia" <ac************@a-znet.comwrote in message
news:%2******************@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.



Nov 22 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.