#include <iostream> #include <string> #include <vector> #include <boost/foreach.hpp> #include <boost/filesystem.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/xpressive/xpressive.hpp>
using namespace std; using namespace boost; using namespace boost::filesystem; using namespace boost::xpressive;
void disk_foreach() { directory_iterator end; for (directory_iterator pos("c://"); pos != end; ++pos) { cout << *pos << endl; }
typedef std::pair<directory_iterator, directory_iterator> dir_range; dir_range dir(directory_iterator("c://"), directory_iterator());
BOOST_FOREACH(auto &x, dir) { cout << x << endl; } }
void GetFilePath(const string& pathName, std::vector <std::string> &recusiveFileVec) { boost::filesystem::recursive_directory_iterator rdi(pathName); boost::filesystem::recursive_directory_iterator end_rdi; recusiveFileVec.empty();
for (; rdi != end_rdi; rdi++) { if (is_directory(*rdi)) { } else { recusiveFileVec.push_back(rdi->path().string()); } } }
void recursive_dir(const path& dir) { directory_iterator end;
for (directory_iterator pos(dir); pos != end; ++pos) { if (is_directory(*pos)) { recursive_dir(*pos); } else { cout << *pos << endl; } } }
void recursive_dir_new(const path& dir) { recursive_directory_iterator end; for (recursive_directory_iterator pos(dir); pos != end; ++pos) { if (pos.level() == 0) { cout << "目录深度: " << pos.level() << " 路径: " << *pos << endl; } } }
boost::optional<path> recursive_find_file(const path& dir, const string& filename) { typedef boost::optional<path> result_type;
if (!exists(dir) || !is_directory(dir)) { return result_type(); }
recursive_directory_iterator end; for (recursive_directory_iterator pos(dir); pos != end; ++pos) { if (!is_directory(*pos) && pos->path().filename() == filename) { return result_type(pos->path()); } } return result_type(); }
std::vector<path> recursive_find_file_regx(const path& dir, const string& filename) { static boost::xpressive::sregex_compiler rc;
if (!rc[filename].regex_id()) { std::string str = replace_all_copy(replace_all_copy(filename, ".", "\\."), "*", ".*"); rc[filename] = rc.compile(str); }
typedef std::vector<path> result_type; result_type vct; if (!exists(dir) || !is_directory(dir)) { return vct; }
recursive_directory_iterator end; for (recursive_directory_iterator pos(dir); pos != end; ++pos) { if (!is_directory(*pos) && regex_match(pos->path().filename().string(), rc[filename])) { vct.push_back(pos->path()); } } return vct; }
size_t my_copy_file(const path& from_dir, const path& to_dir, const string& filename = "*") { if (!is_directory(from_dir)) { cout << "原始文件不能为文件" << endl; return 0; }
auto vec = recursive_find_file_regx(from_dir, filename); if (vec.empty()) { cout << "目录中没有文件,自动跳过拷贝" << endl; return 0; }
path path_ptr; for (auto& ptr : vec) { path_ptr = to_dir / ptr.string().substr(from_dir.string().length()); if (!exists(path_ptr.parent_path())) { create_directories(path_ptr.parent_path()); } cout << "源文件: " << path_ptr.string() << " 拷贝到: " << to_dir.string() << endl; boost::filesystem::copy_file(ptr, path_ptr); } cout << "拷贝总文件数: " << vec.size() << endl; return vec.size(); }
int main(int argc, char *argv[]) { auto ref = recursive_find_file("c:\\lyshark", "123.txt"); if (ref) cout << "找到文件: " << *ref << endl;
auto regx_ref = recursive_find_file_regx("c:\\lyshark", "*.txt");
cout << "找到文件: " << regx_ref.size() << endl; for (boost::filesystem::path &ptr : regx_ref) { cout << "找到文件路径: " << ptr << endl; }
std::vector <std::string> file_path;
GetFilePath("C://backup", file_path); for (int x = 0; x < file_path.size(); x++) { std::cout << file_path[x] << std::endl; }
my_copy_file("c:\\lyshark", "c:\\c");
getchar(); return 0; }
|