Solved

extracting file name from long string

Posted on 2011-02-24
8
294 Views
Last Modified: 2012-05-11
What's best way in regex to extract the file name from the below format of a string


2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps

Open in new window

0
Comment
Question by:NewtonianB
8 Comments
 
LVL 3

Accepted Solution

by:
4runnerfun earned 125 total points
ID: 34971514
Without RegEx, you could split your string with "\" and then use the last index which would contain your filename.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 125 total points
ID: 34971545
That text looks like output of a "dir" or something - which means the filename should always start at the nth position, so you could use String.Substring. Or if the file name never contains spaces, find start of file name with LastIndexOf.
0
 

Author Comment

by:NewtonianB
ID: 34971813
4runnerfun: sometimes there is no path it is just the file name thats why regex provides most flexibility for me

tgerbert: there could be spaces in the path
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:NewtonianB
ID: 34972001
basically all i know is that it will end with  .fps and I need to find the filename (lets actually assume there could be no space in the path or filename)

wether its

2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps


or


2011-02-22 17:49:42 ....A         7114         1088  RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps


I need it to output
RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34972626
Assuming the path/file always starts at the 54th character (and I would guess that it does):
string input = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
string fileName = System.IO.Path.GetFileName(input.Substring(53));

Open in new window


Or, to go from the right-most space to the end of the line:
string input = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
string fileName = System.IO.Path.GetFileName(input.Substring(input.LastIndexOf(' ') + 1));

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 34972720
Hi NewtonianB;

The code in the code snippet should do what you need using Regular Expression.

using System.Text.RegularExpressions;


string testInput = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps" + "\n" +
    @"2011-02-22 17:49:42 ....A         7114         1088  RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";

string pattern = @"(\b[^\s]+\.fps\b)";

MatchCollection mc = Regex.Matches(testInput, pattern);

foreach(Match m in mc)
{
    Console.WriteLine(m.Value);
}

Open in new window


Fernando
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 125 total points
ID: 34978604
Hi
FernandoSoto: solution was good to regex, but it gives the file path when there is any path provided in the source string so its good to add one more line of code to get file name always.

string fileName = null;
string pattern = @"(\b[^\s]+\.fps\b)";
string b=@"2011-02-22 17:49:42 ....A         7114         1088  ErrE\C C\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
 MatchCollection mc = Regex.Matches(b, pattern);
if (mc.Count == 1)
{
fileName = Path.GetFileName(mc[0].Value); // This will return the file name only even when path is matched
}
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34982482
I think you'd want that regular expression to end with "$", e.g. "\b\S+\.fps$", it is possible for a character matching word-boundary to appear in the middle of the filename.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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