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.