Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to test for invalid directory path

Status
Not open for further replies.

jmgrin

MIS
Jan 4, 2002
32
0
0
US
I am working on an application that prompts for a directory path. I use directory.exists(path) to verify that the directory exists, and if it doesn't then create it.

This works fine as long as the path that was entered is valid. However if the path that has been entered is invalid -- for instance path = c;\anydir.
false is returned when I test for directory.exists, so I use: dim di as DirectoryInfo =directory.create(path)

to create the directory. The directory is not created, but directory.exists(path) now returns true. Is there a way of determining that the directory was not created successfully?
 
Well spotted Rick, except that its [app's exe path]\c;\etc

Hope this helps.
 
Thanks for your responsese.

You were absolutely correct about the directory
[app's exe path] \c;\anydir being created.

Can you recommend a good reference for regular expressions?
I've avoiding using them because the syntax is so cryptic and hard to understand.

 
If you look at programming as if it were kung-fu, with each aspect being a differnt technique and training... I would be the equivilant of a training dummy in the mystical art of RegEx-fu.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Apart from the internet, I use:

C# Text Manipulation String Handling and Regular Expressions

from Wrox Press ISBN:1-86100-823-6

Ignore the fact its for C# all the RegEx stuff in there is virtually identical to what you would type in VB.

Hope this helps.
 
The following regular expression works for verifying a directory path:
^([A-Za-z]:)?(\\[\w\s]+)+$
 
That may miss some pathnames because \w only covers A to Z a to z 0 to 9 and _ other valid file/pathname characters would not be picked up.

Hope this helps.
 
\w in .net will cover most of the unicode character space.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
riverguy said:
Regex gives me headaches

I'm so glad I'm not the only one.



Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
earthandfire was correct. One character that wasn't matching is the '-' which was something I wanted to include. I need to do some checking to see what else I might want to include.
 
I'm sure its possible to produce a neater rx but:

^(([A-Za-z]:)|([A-Za-z]:\\)|(\\\\))?[^ \\/:\*\?"<>\|][^/:\*\?"<>\|]*$

as far as I can tell covers every legal possibility except it allows \\ anywhere except after a :

Code:
^                    start of string
(                    beginning of group for Drive:\
([A-Za-z]:)          check for Drive:
|                    or
([A-Za-z]:\\)        check for Drive:\
|                    or
(\\\\)               check for \\
)                    end of Drive:\ group
?                    its not required though
[^ \\/:\*\?"<>\|]    first character of filename can NOT be
                     space \/:*?"<> or |
                     but it must have at least one character
[^/:\*\?"<>\|]       subsequent character must not be
                     /:*?"<> or |
*                    0 or more characters
$                    end of string

The criteria involved to solve this problem is fiddly so I think this is the easiest approach:

Code:
  Private Function ValidateFileName(ByVal fn As String) As Boolean

    Dim filename As String = fn.Trim
    If filename.IndexOf("\\") > 0 Then
      Return False
    Else
      Dim pattern As String = "^(([A-Za-z]:)|([A-Za-z]:\\)|(\\\\))?[^ \\/:\*\?""<>\|][^/:\*\?""<>\|]*$"
      Dim rx As New System.Text.RegularExpressions.Regex(pattern)
      Return rx.IsMatch(filename)
      rx = Nothing
    End If

  End Function

  Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim fn As String = TextBox1.Text.Trim
    MessageBox.Show(ValidateFileName(TextBox1.Text).ToString)

  End Sub


Hope this helps.
 
If ever I get that on my computer I'll reboot.

BTW

Code:
 Return rx.IsMatch(filename)
 rx = Nothing

Will he ever execute the rx = nothing???

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
When that was written 3:30 am approx., it didn't really matter whether or not rx would ever equal nothing. However at 9:30 am, this will ensure that rx really does get set equal to nothing.

Dim rx As New System.Text.RegularExpressions.Regex(pattern)
Dim ok As Boolean = rx.IsMatch(filename)
rx = Nothing
Return ok

so, chrissie, now you wont need to reboot.
 
sorry I was talking about this "^(([A-Za-z]:)|([A-Za-z]:\\)|(\\\\))?[^ \\/:\*\?""<>\|][^/:\*\?""<>\|]*$"
and the rebooting.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
If you think that is bad, you should have seen some of the patterns I produced while trying to prevent the \\ situation. One was probably three times as long (and still didn't work), hence .IndexOf to solve that one.

I first got involved with Regular Expressions a couple of years ago when I had to validate UK postcodes. Without RegEx the code was horrendous (and debugging was even worse). So I thought someone is bound to have done this already, and dutifully let google do its job. Over 80% of those that were supposed to be the 'definitive' regex failed - so I decided to produce my own. I've used them a few times since, but unless you use them on a regular basis, you've got to relearn each time.[smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top