473,801 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Valid file name

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
Jul 6 '06 #1
14 4296
Hi Jack,

Simply do a check to see if file.exists

if file exists it is a valid path if not path was invalid.

--Or to open a file -- prompt the user with a file dialogue box.

hope this helps

Adam

Jack Russell wrote:
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
Jul 6 '06 #2
Adamz5 wrote:
Hi Jack,

Simply do a check to see if file.exists

if file exists it is a valid path if not path was invalid.

--Or to open a file -- prompt the user with a file dialogue box.

hope this helps

Adam

Jack Russell wrote:
>>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

I tried file exists (before posting) with an invalid name - a/b.txt and
it did not throw an exception (much to my surprise)

Jack
Jul 6 '06 #3
try this

regards

Adam

Imports System.IO

Public Class Form1

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
' Dim f As File

If File.Exists("c: \a/b.txt ") Then
MsgBox("validpa th")
Else
MsgBox("invalid path")
End If

End Sub
End Class

Jul 6 '06 #4
This would report an invalid path even if the filename is syntactically
correct but just doesn't exist!

Simon

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Adamz5" <ad****@hotmail .comwrote in message
news:11******** *************@b 68g2000cwa.goog legroups.com...
try this

regards

Adam

Imports System.IO

Public Class Form1

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
' Dim f As File

If File.Exists("c: \a/b.txt ") Then
MsgBox("validpa th")
Else
MsgBox("invalid path")
End If

End Sub
End Class

Jul 6 '06 #5
Okay here you go...
full code to check file name.
'place a textbox on form write text into it and then press button 1
'place this code in the button1_click event to check that the filename
entered in textbox 1 is fine

Dim uservalue As String
uservalue = TextBox1.Text
Dim b As Boolean = False

b = uservalue.Conta ins("/") Or uservalue.Conta ins("?") _
Or uservalue.Conta ins(":") _
Or uservalue.Conta ins("*") _
Or uservalue.Conta ins("""") _
Or uservalue.Conta ins("<") _
Or uservalue.Conta ins(">") _
Or uservalue.Conta ins("|") _
Or uservalue.Conta ins("?")

If b = False Then
MsgBox("Valid Filename")
Else
MsgBox("Invalid Filename")
End If

By the way this is the long way (not that long is it?)

Anyway hope this helps

Regards

Adam

Jul 6 '06 #6
Which version ?

2.0 has a System.IO.Path. GetFullPath that should raise an Argument exception
if the path contains invalid chars (the same class exposes info about
forbidden chars for file names and paths).

--
Patrice

"Jack Russell" <ja***@norubbis h.tpg.com.aua écrit dans le message de news:
OV************* *@TK2MSFTNGP03. phx.gbl...
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

Jul 6 '06 #7
Adamz5 wrote:
Okay here you go...
full code to check file name.
'place a textbox on form write text into it and then press button 1
'place this code in the button1_click event to check that the filename
entered in textbox 1 is fine

Dim uservalue As String
uservalue = TextBox1.Text
Dim b As Boolean = False

b = uservalue.Conta ins("/") Or uservalue.Conta ins("?") _
Or uservalue.Conta ins(":") _
Or uservalue.Conta ins("*") _
Or uservalue.Conta ins("""") _
Or uservalue.Conta ins("<") _
Or uservalue.Conta ins(">") _
Or uservalue.Conta ins("|") _
Or uservalue.Conta ins("?")

If b = False Then
MsgBox("Valid Filename")
Else
MsgBox("Invalid Filename")
End If

By the way this is the long way (not that long is it?)

Anyway hope this helps

Regards

Adam
Yes that is basically what I did but knowing MS there are other things
that make a file name invalid so I thought there might be a system
function that checked all possibilities. Anyway thanks for everyones
thoughts.

Jack
Jul 6 '06 #8
Try Path.GetFileNam e or Path.GetDirecto ryName (or both). Both will throw an
exception if invalid characters are found in the string. If you need a list
of invalid characters you can get them by using Path.GetInvalid FileNameChars
and Path.GetInvalid PathChars

/claes
"Jack Russell" <ja***@norubbis h.tpg.com.auwro te in message
news:OV******** ******@TK2MSFTN GP03.phx.gbl...
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

Jul 6 '06 #9
Claes Bergefall wrote:
Try Path.GetFileNam e or Path.GetDirecto ryName (or both). Both will throw an
exception if invalid characters are found in the string. If you need a list
of invalid characters you can get them by using Path.GetInvalid FileNameChars
and Path.GetInvalid PathChars

/claes
"Jack Russell" <ja***@norubbis h.tpg.com.auwro te in message
news:OV******** ******@TK2MSFTN GP03.phx.gbl...
>>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


Thanks for that
Jul 6 '06 #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
5
1765
by: Sky | last post by:
What makes something a valid DataSource? What methods/iterators/etc? Why do I ask? I do understand that a DataSet is based on an XML structure...but it's too table structured for what I am thinking... Can one read in a an xml file that has various embedded nodes (ie: records that have children records as XML does best) -- possibly not all the same length (ie, dif 'column count' for certain 'rows') and try to shove that into a...
1
6153
by: David Lozzi | last post by:
Hello, My webservice is receiving the following error. It runs fine on my local development machine,but when moved to production it errors. I found one KB about this error but it was in regards to .Net 1.0, this server is 1.1. Thanks! Configuration Error Description: An error occurred during the processing of a configuration file
1
14462
by: Andy | last post by:
Hi, I created a sample web service and tried calling it from my VBScript. Here it is: ---------------------------------------------------------------------------- --------------- '-- Declare variables dim fs
10
16432
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
4
5570
by: Jatinder | last post by:
Hi, I am trying to grant connect privilege to a user present on my O.S. (Windows) using following statement. GRANT CONNECT ON DATABASE TO user "user1" now when I execute this statemnt from db2 clp(ofcourse prefixing db2) it works fine, but when I put it inside an sql file and then when I try to run the file using db2 -tvf, it gives me following error msg
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
10293
by: yogitha | last post by:
For the below code i am getting an error like : --------------------------------------------------------------- Warning: fread(): supplied argument is not a valid stream resource in D:\Public_Site\multifileupload\welcome_addfiles.php on line 28 ------------------------------------------------------------ i am trying many ways by giving diff arguments in fread() --------------------------------------------------- suggest me , wat error...
10
5941
by: Academia | last post by:
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.
0
9697
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
10515
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
10291
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
6827
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();...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.