Solved

Need Regx for file path validation

Posted on 2013-06-12
8
271 Views
Last Modified: 2013-10-29
Hi,
I need the regx pattern to validate the following file format

\\test\test$\TEST.xls
\\server\share\folder\myfile.txt
\\server\share\myfile.txt
\\123.123.123.123\share\folder\myfile.txt
c:\folder\myfile.txt
c:\folder\myfileWithoutExtension
C:\folder\myfile.file\
C:\folder\file1.File\
0
Comment
Question by:sam_net
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Accepted Solution

by:
angus_young_acdc earned 500 total points
ID: 39240876
Validate for what?  That the folders / paths exist?

If so why not just check if the file / folder exist with existing functions?  Path would be each line of the file you're reading for the paths

if (Directory.Exists(path))
{

}

Open in new window


if (File.Exists(path))
{
   return true;
}

Open in new window

0
 

Author Comment

by:sam_net
ID: 39240883
I am getting file path as input.

i.e. from Textbox

need to validate the give input as valid for or not... and need to match following format
\\test\test$\TEST.xls
\\server\share\folder\myfile.txt
\\server\share\myfile.txt
\\123.123.123.123\share\folder\myfile.txt
c:\folder\myfile.txt
c:\folder\myfileWithoutExtension
C:\folder\myfile.file\
C:\folder\file1.File\
0
 
LVL 10

Expert Comment

by:Monica P
ID: 39241333
Article Give u the concept in detail : http://www.codeproject.com/Tips/216238/Regular-Expression-to-Validate-File-Path-and-Exten

^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$

Open in new window



^(?:[\w]\:|\\) -- Begin with x:\ or \\

[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)

(txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)

Matches:
\\192.168.0.1\folder\file.pdf
\\192.168.0.1\my folder\folder.2\file.gif
c:\my folder\abc abc.docx
c:\my-folder\another_folder\abc.v2.docx

Non-Matches:
\\192.168.0.1\folder\fi<le.pdf
\\192.168.0.1\folder\\file.pdf
\\192.168.0.1\my folder\folder.2\.gif
c:\my folder\another_folder\.docx
c:\my folder\\another_folder\abc.docx
c:\my folder\another_folder\ab*c.v2.docx
c:\my?folder\another_folder\abc.v2.docx
file.xls

Test it:
http://regexpal.com/
http://regexlib.com/



http://regexlib.com/Search.aspx?k=path&AspxAutoDetectCookieSupport=1
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:sam_net
ID: 39241436
Thanks for your inputs.

Here the file type externsions were predefined.. But i need to allow all valid extension.

i.e C:\Test\sample.csv
i.e. C:\Test\sample.png

So i expecting Regx to support all the valida extension along with above format.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39243090
Try the following:

^(?:[a-zA-Z]:|\\)(?:\\(?!\.)[a-zA-Z0-9^&'@{}\[\],$=!#()%.+~_ -]+(?:\\(?!\\))?)+$

Open in new window

0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39252694
Sam_net

If you are getting it from a textbox using Directory.Exists("") and File.Exists("") will still work.  If you have a file called Test.txt on your C:\ then File.Exists("C:\Test.txt") will return true, if it doesn't (because the path isn't valid or whatever) then it will return false.
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39252695
Just seen your last post, also File.Exists(") will take any extension.  Just pass the path to the file and it will just check it is there, it doesn't care about the type.
0
 

Author Closing Comment

by:sam_net
ID: 39608503
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net, radiobuttonlist, c# 3 69
Inserting LocalDB Table to SQL Server C# 3 49
C# DateTime Help 6 46
.NET Redemption/Outlook Examples 3 32
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question