473,800 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5940
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.FileI nfo("[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
GetInvalidFileN ameChars and GetInvalidPathC hars 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.IndexOfA ny. If it returns anything other
then negative 1, then you have a bad filename:

dim bfc() as char = path.getinvalid filenamechars()

if filename.indexo fany(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******** *************** ***********@w28 g2000hsf.google groups.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.FileI nfo("[Path]")
if fi.Exists then
'do work
end if

Nov 21 '07 #4
I had read the help for GetInvalidPathC hars 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*********@co mcast.netwrote in message
news:f0******** *************** ***********@f13 g2000hsa.google groups.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
GetInvalidFileN ameChars and GetInvalidPathC hars 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.IndexOfA ny. If it returns anything other
then negative 1, then you have a bad filename:

dim bfc() as char = path.getinvalid filenamechars()

if filename.indexo fany(bfc) -1 then
' illegal file name
end if

--
Tom Shelton

Nov 21 '07 #5
Also GetInvalidFilen ameChars 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******** ******@TK2MSFTN GP06.phx.gbl...
>I had read the help for GetInvalidPathC hars 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*********@co mcast.netwrote in message
news:f0******** *************** ***********@f13 g2000hsa.google groups.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
GetInvalidFile NameChars and GetInvalidPathC hars 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.IndexOfA ny. If it returns anything other
then negative 1, then you have a bad filename:

dim bfc() as char = path.getinvalid filenamechars()

if filename.indexo fany(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******** **********@TK2M SFTNGP05.phx.gb l...
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 GetInvalidFilen ameChars 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.inv alidwrote in message
news:uW******** ******@TK2MSFTN GP02.phx.gbl...
Academia wrote:
>Also GetInvalidFilen ameChars 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******** *************** ***********@mic rosoft.com...
try to create a file with that name/path, then delete it.... XD

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14540
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 dialog. The strange thing is that sometimes the save dialogue
14
4296
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
4274
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 that I consider to be valid characters... valid = ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' if I have a string called fname I want to go through each character in
2
3986
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"; ifstream fin( file ); The error at last line is:
0
9695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10514
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10287
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10260
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10042
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6826
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2956
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.