Solved

Need Regx for file path validation

Posted on 2013-06-12
8
262 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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