C++11 如何获取根路径下的特定子文件夹路径?
我想获取根目录下特定文件夹的位置。 例如,我有一个根目录为C++11 如何获取根路径下的特定子文件夹路径?,c++11,visual-c++,C++11,Visual C++,我想获取根目录下特定文件夹的位置。 例如,我有一个根目录为C:\Dummy,我在这个文件夹中有一个子目录: C:\Dummy\10\20\MyFolder 现在我想获得目录C:\Dummy下的子目录MyFolder的路径 我将编写一个函数,其中我将传递两个输入: 1) “根文件夹”,即C:\Dummy 2) “子目录名”,即MyFolder String fun(string RootFolderPath, string subDirName) { //if any of the sub
C:\Dummy
,我在这个文件夹中有一个子目录:
C:\Dummy\10\20\MyFolder
现在我想获得目录C:\Dummy
下的子目录MyFolder
的路径
我将编写一个函数,其中我将传递两个输入:
1) “根文件夹”,即C:\Dummy
2) “子目录名”,即MyFolder
String fun(string RootFolderPath, string subDirName)
{
//if any of the sub directories consists of `subDirName` then return the
//path
return subDirPath;
}
有没有办法做到这一点
请帮助我解决此问题。使用实验标准库,可以按以下方式完成:
#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 "";
}
#包括
命名空间fs=std::实验::文件系统;
字符串搜索路径(常量字符串和根、常量字符串和搜索)
{
fs::path root\u path(root);
fs::路径搜索\u路径(搜索);
for(auto&p:fs::递归目录迭代器(根路径))
{
if(fs::is_目录(p.status()))
{
if(p.path().filename()==搜索)
返回p.path().string();
}
}
返回“”;
}
否则,必须使用特定于windows.api的FindFirstFile()和FindTextFile()来执行遍历。或者是Boost库 通过连接RootFolderPath和subDirName来创建完整的目录路径(不要忘记在两者之间插入“\”。并使用以下2个Windows API:
auto bDirExists = (::PathFileExists(path) && ::PathIsDirectory(path));
你希望得到什么
10\20\MyFolder
?@Kane,这就是我所期望的“C:\Dummy\10\20\MyFolder”。所以您需要类似于std::string findDirectory(const std::string&root,const std::string&directory)
的东西,它将在文件系统上找到相应的目录并返回其路径?能否请你更新你的问题,提供更多关于你有哪些输入数据以及你希望得到哪些输出的详细信息?@Kane,类似于你提到的。我详细地更新了我的问题。谢谢,现在问题更清楚了。请检查下面acraig5075的答案。如果您想使用WinAPI,可能会在此处找到答案:。