Nelson T Joseph
Nelson T Joseph

Reputation: 2763

How to get the file name from the full path of the file, in vc++?

I need to get the name of a file from its full path, in vc++. How can I get this? I need only the file name. Can I use Split method to get this? If not how can I get the file name from the full path of the file?

Upvotes: 0

Views: 2048

Answers (3)

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );

See Path::GetFileName Method

Upvotes: 1

Richard
Richard

Reputation: 108975

Find the last \ or /1 using one of the standard library string/char * search methods. Then extract the following text. Remember to special case where the / or \ is the last character.


1 The Windows API, for most purposes2 supports both.

1 The exception is when using long paths starting \\?\ to break 260 character limit on paths.

Upvotes: 0

Related Questions