从所有列出的文件和子目录中更改目录



我目前正在编写一个程序,列出当前目录中的所有文件和子目录。

我正在努力更改目录的能力,因此在列出所有目录和子目录后,我需要能够更改目录。在我的代码中,我有fs::is_directory(myPath)告诉我这个文件是否是一个目录,如果是,我想指向我的路径,然后从内部打印文件。我甚至可以通过使用parent_path来做到这一点,但我不知道该怎么做。

#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main()
{
fs::path myPath("C:\Users\xyz\Desktop\things");
cout << "your path is : " << myPath << endl;
cout << "name of this directory is : "<< myPath.filename() << endl;
cout << fs::is_directory(myPath) << endl;                        
cout << myPath.parent_path() << endl;                                             
for(auto file : fs::directory_iterator(myPath)) {
cout << file << " " << fs::is_directory(file) << endl;
}
return 0;
}

你可以用递归来实现它。使用循环创建listDirectory(std::string && path)函数,但为文件和目录添加不同的行为。 这是一个伪代码:

void listDirectory(const std::string &path( { for (auto file : fs::d irectory_iterator(path(( { if (fs::is_directory(file(( { 列表目录(文件(; } 否则 { std::cout <<文件; } } }

最新更新