Solved

Need Regx for file path validation

Posted on 2013-06-12
8
257 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

770 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