2017-07-24 94 views
0

我想获取根目录下特定文件夹的位置。 例如,我有一个根目录C:\Dummy和我有这个文件夹里面有个子目录:如何获取根路径下的特定子文件夹路径?

C:\虚拟\ 10 \ 20 \ MyFolder文件

现在,我想目录C:\Dummy下子目录MyFolder的路径。

我会写的函数,其中我将通过两个输入:即C:\Dummy 2)“子目录名称” 1)“根文件夹”,即MyFolder

String fun(string RootFolderPath, string subDirName) 
{ 

    //if any of the sub directories consists of `subDirName` then return the 
    //path 
    return subDirPath; 
} 

有没有什么办法可以实现这个目标?

请帮我解决这个问题。

+0

你期望得到什么? '10个\ 20 \ MyFolder'? – Kane

+0

@Kane,这是我期望的“C:\ Dummy \ 10 \ 20 \ MyFolder”。 – Siva

+0

所以你想要'std :: string findDirectory(const std :: string&root,const std :: string&directory)'这会在文件系统上找到相应的目录并返回它的路径?您能否更新您的问题,并提供更多关于您有什么输入数据以及您期望输出什么信息的详细信息? – Kane

回答

0

使用实验filesystem标准库也可以如下进行:

#include <experimental\filesystem> 

namespace fs = std::experimental::filesystem; 

string search_path(const string &root, const string &search) 
{ 
    fs::path root_path(root); 
    fs::path search_path(search); 

    for (auto &p : fs::recursive_directory_iterator(root_path)) 
     { 
     if (fs::is_directory(p.status())) 
      { 
      if (p.path().filename() == search) 
       return p.path().string(); 
      } 
     } 

    return ""; 
} 

否则,您必须使用特定windows.api像file management functions用FindFirstFile()和FindNextFile()做遍历。或者可能是Boost filesystem库。

0

通过连接RootFolderPathsubDirName(不要忘记在这两者之间插入“\”)创建完整的目录路径。并使用以下2个Windows API:

​​3210