Solved

Need Regx for file path validation

Posted on 2013-06-12
8
238 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 74

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now