Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Getting the file part or file name part using regex for vb.net

I have been trying to be able to use a regex to parse out a filename and extension from a long path string...For Example

C:\My Documents\lng path\my file.csv

I want to get "my file.csv" returned.  So far  I can get the path with...
^([a-zA-Z]\:|\\)\\([^\\]+\\)*

This one gets the .csv but I need "my file.csv"
(?<=^([a-zA-Z]\:|\\)\\([^\\]+\\))*[\.](CSV)+

I have been going crazy trying to figure it out...
Thanks in advance
0
kenadelglass
Asked:
kenadelglass
1 Solution
 
MageDribbleCommented:
Use the following functions:

Path.GetDirectoryName(your string here)
Path.GetFileNameWithoutExtension(your string here)

check out the Path object in MSDN - it'll solve all your needs.
0
 
MageDribbleCommented:
here is the link.  It is the Path class.

http://msdn2.microsoft.com/en-us/library/3bdzys9w(vs.80).aspx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use the FileInfo class:

        Dim path As String = "C:\My Documents\lng path\my file.csv"

        Dim fi As New System.IO.FileInfo(path)
        MsgBox(fi.Name)
        MsgBox(fi.Extension)

        ' Oddly enough..theres no GetFileNameWithoutExtension() in the FileInfo class!
        MsgBox(fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length))

        MsgBox(fi.DirectoryName)

See here:
http://msdn2.microsoft.com/en-us/library/system.io.fileinfo_members(VS.80).aspx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bchoorCommented:
MageDribble and IdleMind solutions are the most straight forward (for sure) to retrieve the filename. However, if you want to use RegEx here's a few patterns I came up with - I haven't tested too many derivations of filenames

        Dim path As String = System.Text.RegularExpressions.Regex.Match(FullPath, "^[\w\W]+\\").Value
        Dim fileName As String = System.Text.RegularExpressions.Regex.Match(FullPath, "\\[\w\W][^\\]+$").Value
        Dim fileExt As String = System.Text.RegularExpressions.Regex.Match(fileName, "\.[\w\W][^\.]+$").Value

        Debug.Print("Path: {0}", path)
        Debug.Print("FileName: {0}", fileName.Trim("\"))   'Remove the first backslash if it exists
        Debug.Print("Extension: {0}", fileExt.Trim("."))       'Remove the first period if it exists

One that I tried, that works
?GetFileName("C:\afasdf\asdfasdf\asdfasdfsad\kasf.asdfasdf\afkasjdfasd32423423*(@#4234234).esf.sdfafsdf")
Path: C:\afasdf\asdfasdf\asdfasdfsad\kasf.asdfasdf\
FileName: afkasjdfasd32423423*(@#4234234).esf.sdfafsdf
Extension: sdfafsdf

HTH
~BC
0
 
kenadelglassAuthor Commented:
Thanks to everyone...I was really looking for a regex answer, I know how to do it with System.IO.Path but wanted to figure it out with a regex.  One question, when I use
[^\\]+$ on my path of  C:\My Documents\lng path\my file.csv I get exactly "my file.csv" which is what I was looking for

I can't figure out why that is. [^\\]+ tells me get the specific match starting at the beginning of the document which uses the escape \ followed by the \ which would mean find the \ and with the + meaning one or more - so wouldn't it return all the slashes from the beginning of the document?  Why then does it get me everything in between?  What am I missing here?
0
 
bchoorCommented:
"\\[\w\W][^\\]+$").

if you break it down
\\ ==> means the backslash
[\w\W] ==> all characters (both alphanumeric and non-alphanumeric)
[^\\] ==> Exclude matches that have a backslash at any index in the string
+$ ==> repeat from the end of the string

so if you tried "\\[\w\W]+$"
you would get "\My Documents\lng path\my file.csv", so by excluding a match that has a backslash, you get just the filename i.e. "\my file.csv" then u just have to trim the beginning backslash

HTH
~BC


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now