How to get file name from path?

I need some regex experts for an extra hand. :)

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

My question is: how do I get the last one: file name?

For example, on the way:

C: \ A \ B \ C \ D \ E \ fgh.ddj

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

+3
source share
3 answers

You do not need regular expressions, you can do it just like this: its helper function system.io:

myfilename = Path.GetFileName(mypath);
+25
source

You can also use FileInfo. When using FileInfo, it really 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"

, , , ..

+1

If you have perl installed, 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;
-4
source

Source: https://habr.com/ru/post/1709634/


All Articles