How to get parent directory from file path

12 03 2013

We usually use file paths and directory paths in our code. Sometimes they have to be processed, like how we can parse parent directory from full file path.

The Windows API PathRemoveFileSpec removes the trailing file name and backslash from a path.

#include "Shlwapi.h"

void main()
  TCHAR filePath = "C:\\TEST\\sample.txt";
  PathRemoveFileSpec (filePath); //filePath becomes "C:\\TEST"

Don’t forget to include shlwapi.lib to project settings.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: