Getting a directory name from a filename

C++FileDirectory

C++ Problem Overview


I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in unmanaged C++. In C# I would do something like this:

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;

Is there a function that can be used in unmanaged C++ to extract the path from the filename?

C++ Solutions


Solution 1 - C++

Using Boost.Filesystem:

boost::filesystem::path p("C:\\folder\\foo.txt");
boost::filesystem::path dir = p.parent_path();

Solution 2 - C++

Example from http://www.cplusplus.com/reference/string/string/find_last_of/

// string::find_last_of
#include <iostream>
#include <string>
using namespace std;

void SplitFilename (const string& str)
{
  size_t found;
  cout << "Splitting: " << str << endl;
  found=str.find_last_of("/\\");
  cout << " folder: " << str.substr(0,found) << endl;
  cout << " file: " << str.substr(found+1) << endl;
}

int main ()
{
  string str1 ("/usr/bin/man");
  string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}

Solution 3 - C++

In C++17 there exists a class std::filesystem::path using the method parent_path.

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    for(fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."})
        std::cout << "The parent path of " << p
                  << " is " << p.parent_path() << '\n';
}

Possible output:

The parent path of "/var/tmp/example.txt" is "/var/tmp"
The parent path of "/" is ""
The parent path of "/var/tmp/." is "/var/tmp"

Solution 4 - C++

There is a standard Windows function for this, PathRemoveFileSpec. If you only support Windows 8 and later, it is highly recommended to use PathCchRemoveFileSpec instead. Among other improvements, it is no longer limited to MAX_PATH (260) characters.

Solution 5 - C++

Why does it have to be so complicated?

#include <windows.h>

int main(int argc, char** argv)         // argv[0] = C:\dev\test.exe
{
    char *p = strrchr(argv[0], '\\');
    if(p) p[0] = 0;

    printf(argv[0]);                    // argv[0] = C:\dev
}

Solution 6 - C++

 auto p = boost::filesystem::path("test/folder/file.txt");
 std::cout << p.parent_path() << '\n';             // test/folder
 std::cout << p.parent_path().filename() << '\n';  // folder
 std::cout << p.filename() << '\n';                // file.txt

You may need p.parent_path().filename() to get name of parent folder.

Solution 7 - C++

Use boost::filesystem. It will be incorporated into the next standard anyway so you may as well get used to it.

Solution 8 - C++

_splitpath is a nice CRT solution.

Solution 9 - C++

I'm so surprised no one has mentioned the standard way in Posix

Please use basename / dirname constructs.

man basename

Solution 10 - C++

Standard C++ won't do much for you in this regard, since path names are platform-specific. You can manually parse the string (as in glowcoder's answer), use operating system facilities (e.g. http://msdn.microsoft.com/en-us/library/aa364232(v=VS.85).aspx ), or probably the best approach, you can use a third-party filesystem library like boost::filesystem.

Solution 11 - C++

Just use this: ExtractFilePath(your_path_file_name)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon TackaburyView Question on Stackoverflow
Solution 1 - C++Khaled AlshayaView Answer on Stackoverflow
Solution 2 - C++corsiKaView Answer on Stackoverflow
Solution 3 - C++Alessandro JacopsonView Answer on Stackoverflow
Solution 4 - C++Andreas RejbrandView Answer on Stackoverflow
Solution 5 - C++toster-cxView Answer on Stackoverflow
Solution 6 - C++srbcheema1View Answer on Stackoverflow
Solution 7 - C++Edward StrangeView Answer on Stackoverflow
Solution 8 - C++Ofek ShilonView Answer on Stackoverflow
Solution 9 - C++Utkarsh KumarView Answer on Stackoverflow
Solution 10 - C++CogwheelView Answer on Stackoverflow
Solution 11 - C++fxPiotrView Answer on Stackoverflow