2017-07-24 93 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