How to extract directory path from a full file path?

 

It’s often necessary to extract the directory path from a whole file path. Path Handling functions are truly handy by providing lot of path related helper functions.

You can use PathRemoveFileSpec function to do this. Here’s modified version of the original sample provided at msdn website.

[sourcecode language='cpp']
#include
#include “Shlwapi.h”

#pragma comment (lib, “shlwapi.lib” )

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
// Path to include file spec.
TCHAR buffer_1[] = _T( “C:\\TEST\\sample.txt” );

// Print the path with the file spec.
_tprintf( _T( “The path with file spec is : %s \n” ) ,buffer_1 );

// Call to “PathRemoveFileSpec”. This will modify the original buffer
PathRemoveFileSpec(buffer_1);

// Print the path without the file spec.
_tprintf( _T( “\nThe path without file spec is : %s \n” ) , buffer_1 );
return 0;
}
[/sourcecode]

 
This entry was posted in Uncategorized. Bookmark the permalink.