tags:

views:

276

answers:

3

Hello,

I need some Regular expression experts for an extra hand. :)

I have different paths,different folders,different amount of folders.

My question:How do I get the last thing - the filename?

For example in the path:

C:\a\b\c\d\e\fgh.ddj

How do I get "fgh.ddj" with regular expressions?

+23  A: 

You don't need regex's, you can do it just like this, its a system.io helper function:

myfilename = Path.GetFileName(mypath);
Chris
And if you need more than just the filename then FileInfo finfo = new FileInfo(path); Then info contains path, fullname, directoryname and more...
Vinko Vrsalovic
As do methods on Path: Path.GetDirectoryName, Path.GetFullPath, etc.
Michael Petrotta
@Vinko: FYI, there are static helper functions for most of that within System.IO.Path, System.IO.File and System.IO.Directory. Most of the time you can write a one-liner instead of using FileInfo.
0xA3
A: 

If you have perl installed, then you can try something like this...

#!/usr/bin/perl

use strict;

my $fullname = 'C:\a\b\c\d\e\fgh.ddj';
my $file = (split /\\/, $fullname)[-1];
print $file;
bichonfrise74
humm(?!)... C# is one of the tags, right? ...
balexandre
A: 

You can also use FileInfo. When using FileInfo, it actually doesn't matter if the file is present or not.

var fileInfo = new FileInfo("C:\a\b\c\d\e\fgh.ddj");
var fileName = fileInfo.Name;
//this returns "fgh.ddj"

If the file is present, of course there's lots of info about file size, last accessed, etc.

ChrisW