C++ 删除除特定文件夹外的所有文件夹
我已经有了一个boost功能,可以一次删除一个文件夹<代码>全部删除()代码> 文件夹列表包括:C++ 删除除特定文件夹外的所有文件夹,c++,directory,boost-filesystem,C++,Directory,Boost Filesystem,我已经有了一个boost功能,可以一次删除一个文件夹全部删除() 文件夹列表包括: folder1 folder2 folder3 folder4 folder5 我想用上面的函数将它们全部删除,但保留folder2和folder5。我实际上找到了两种方法 首先,我将文件夹列表放入一个数组中 第一种方法:使用函数在字符串数组中查找子字符串,然后将其删除 第二种方法:使用strcmp与我的字符串数组进行比较,然后删除找到的搜索标记 这是最后的代码: // simple_ls program fo
folder1
folder2
folder3
folder4
folder5
我想用上面的函数将它们全部删除,但保留folder2和folder5。我实际上找到了两种方法 首先,我将文件夹列表放入一个数组中 第一种方法:使用函数在字符串数组中查找子字符串,然后将其删除 第二种方法:使用strcmp与我的字符串数组进行比较,然后删除找到的搜索标记 这是最后的代码:
// simple_ls program form boost examples
// http://www.boost.org/doc/libs/1_52_0/libs/filesystem/example/simple_ls.cpp
#define BOOST_FILESYSTEM_VERSION 3
// We don't want to use any deprecated features
#ifndef BOOST_FILESYSTEM_NO_DEPRECATED
# define BOOST_FILESYSTEM_NO_DEPRECATED
#endif
#ifndef BOOST_SYSTEM_NO_DEPRECATED
# define BOOST_SYSTEM_NO_DEPRECATED
#endif
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include <iostream>
#include <cstring>
using namespace std;
using namespace boost::filesystem;
unsigned long dir_count = 0;
void RemoveSub(string& sInput, const string& sub) {
string::size_type foundpos = sInput.find(sub);
if ( foundpos != string::npos )
sInput.erase(sInput.begin() + foundpos, sInput.begin() + foundpos + sub.length());
}
int listDir(string d) {
d.erase(
remove( d.begin(), d.end(), '\"' ),
d.end()
); //Remove Quotes
if (!is_directory(d)) {
cout << "\nNot found: " << d << endl;
return 1;
}
directory_iterator end_iter;
for (directory_iterator dir_itr(d);
dir_itr != end_iter;
++dir_itr) {
if (is_directory(dir_itr->status())) {
++dir_count;
string v = dir_itr->path().filename().string();
v.erase(
remove( v.begin(), v.end(), '\"' ),
v.end()
);
string m[] = { v };
string mm = m[0].c_str();
RemoveSub(mm, "folder2"); // Keep folder2
RemoveSub(mm, "folder5"); // Keep folder5
/*
if( strcmp(m[0].c_str(), "folder2") == 0 ) mm.erase (mm.begin(), mm.end()); // Keep folder2
if( strcmp(m[0].c_str(), "folder5") == 0 ) mm.erase (mm.begin(), mm.end()); // Keep folder5
*/
if(!mm.empty()) { // Remove folders
cout << "\nRemoving: " << mm << " ...";
remove_all(d+"/"+mm);
}
}
}
return 0;
}
int main(int argc, char* argv[]) {
string i;
cout << "\nx: Exit\n\nDelete all folders in: ";
getline(cin,i);
if(i=="X" || i=="x") return 0;
if(i.empty()) return 0;
listDir(i); //Call our function
return 0;
}
//简单的程序表单boost示例
// http://www.boost.org/doc/libs/1_52_0/libs/filesystem/example/simple_ls.cpp
#定义BOOST_文件系统_版本3
//我们不想使用任何不推荐的功能
#ifndef BOOST\u文件系统\u否\u已弃用
#定义BOOST\u文件系统\u否\u已弃用
#恩迪夫
#ifndef增压系统不推荐使用
#定义BOOST\u系统\u否\u已弃用
#恩迪夫
#包括“boost/filesystem/operations.hpp”
#包括“boost/filesystem/path.hpp”
#包括“boost/progress.hpp”
#包括
#包括
使用名称空间std;
使用名称空间boost::filesystem;
无符号长目录计数=0;
void RemoveSub(字符串和输入、常量字符串和子字符串){
字符串::size\u type foundpos=sInput.find(sub);
if(foundpos!=字符串::npos)
擦除(sInput.begin()+foundpos,sInput.begin()+foundpos+sub.length());
}
int listDir(字符串d){
d、 抹去(
删除(d.begin(),d.end(),“\”),
d、 完()
);//删除引号
如果(!is_目录(d)){
难道你不能循环浏览文件夹并检查它们是否不是folder2
或folder5
,如果不是删除它们吗?将你想保留的文件夹加载到std::集中,然后将其传递给一个增强的删除功能,该功能只删除不在该集中的项目。如果你递归到子文件夹中,你可能需要发挥创造力。写一篇有趣的文章该操作获取一个“要保留的文件夹”列表,并将每个“可能删除此”与“要保留的文件夹”列表进行比较,如果它在保留列表中,则不要删除它(或者在删除后写上“哈哈,无论如何删除了它”,这是不正当的!)我已经找到了答案,但我还不能发布它,因为我是一个新用户…@Peteragent5好的,我给你一个+1。你应该在你的答案中显示一些工作代码。这个解决方案对字符串操作的严重依赖使得这个(1)很难阅读和(2)容易出错。您应该尝试合并@WhozCraig的提示,即使用集合存储要保存的目录,并更多地使用boost::filesystem为您提供的类型和函数,而不是摆弄完整路径的字符串表示。