473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
10 5898
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
14
by: Jack Russell | last post by:
Is there a simple function to test if a string is a valid file name (i.e does not contain illegal characters etc) other than doing it the long way? Thanks Jack Russell
10
by: SpreadTooThin | last post by:
Hi I'm writing a python script that creates directories from user input. Sometimes the user inputs characters that aren't valid characters for a file or directory name. Here are the characters...
2
by: mahmoodn | last post by:
I want to pass a string as file name to a ifstream object, but I can't. char cnt_string = ""; string f = "ss_"; int cnt = 1; string seq = itoa(cnt, cnt_string, 10); string file = f+seq+".txt";...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.