Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need Regx for file path validation

Posted on 2013-06-12
8
Medium Priority
?
280 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 1500 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
Industry Leaders: 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!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

916 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