#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
bool scanFilesUseRecursive(const string& rootPath,vector<string>& container=*(new vector<string>()))
{
namespace fs = boost::filesystem;
fs::path fullpath (rootPath, fs::native);
vector<string> &ret = container;
//判断路径是否存在
if(!fs::exists(fullpath))
{
return false;
}
fs::recursive_directory_iterator end_iter;
for(fs::recursive_directory_iterator iter(fullpath);iter!=end_iter;iter++)
{
try{
//是文件名&&.jpg
if (!fs::is_directory( *iter ) &&(fs::extension(*iter)==".jpg"||fs::extension(*iter)==".JPG"||fs::extension(*iter)==".jpeg"||fs::extension(*iter)==".JPEG") )
{
//
std::string jpgstring=iter->path().string();
ret.push_back(jpgstring);
std::cout << *iter << " is a file" << std::endl;
}
} catch ( const std::exception & ex )
{
std::cerr << ex.what() << std::endl;
continue;
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::string path="D:\\TestTool\\";
vector<string> FileName;
scanFilesUseRecursive(path,FileName);
return 0;
}
常用函数
函数名 作用
system_complete(path); 返回完整路径(相对路径+当前路径)
exists(path); 目录是否存在
is_directory(path);
is_directory(file_status); 是否是路径
is_regular_file(path);
is_regular_file(file_status); 是否是普通文件
is_symlink(path);
is_symlink(file_status); 是否是一个链接文件
file_status status(path); 返回路径名对应的状态
initial_path(); 得到程序运行时的系统当前路径
current_path(); 得到系统当前路径
current_path(const Path& p); 改变当前路径
space_info space(const Path& p); 得到指定路径下的空间信息,space_info 有capacity, free 和 available三个成员变量,分别表示容量,剩余空间和可用空间。
last_write_time(const Path& p); 最后修改时间
last_write_time(const Path& p, const std::time_t new_time); 修改最后修改时间
bool create_directory(const Path& dp); 建立路径
create_hard_link(const Path1& to_p, const Path2& from_p);
error_code create_hard_link(const Path1& to_p, const Path2& from_p, error_code& ec); 建立硬链接
create_symlink(const Path1& to_p, const Path2& from_p);
create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec); 建立软链接
remove(const Path& p, system::error_code & ec = singular ); 删除文件
remove_all(const Path&p); 递归删除p中所有内容,返回删除文件的数量
rename(const Path1& from_p, const Path2&to_p); 重命名
copy_file(const Path1& from_fp, const Path2& to_fp); 拷贝文件
create_directories(const Path &p); 建立路径
directory_iterator :只支持本层目录遍历
recursive_directory_iterator :支持深度遍历