void testFileSystem()
{
boost::filesystem::path path("/test/test1"); //初始化
boost::filesystem::path old_cpath = boost::filesystem::current_path(); //取得当前程序所在文件夹
boost::filesystem::path parent_path = old_cpath.parent_path();//取old_cpath的上一层父文件夹路径
boost::filesystem::path file_path = old_cpath / "file"; //path支持重载/运算符
std::string file_dir = file_path.string();
if (boost::filesystem::exists(file_path)) //推断文件存在性
{
std::string strPath = file_path.string();
int x = ;
}
else
{
//文件夹不存在;
boost::filesystem::create_directory(file_path); //文件夹不存在。创建
}
bool bIsDirectory = boost::filesystem::is_directory(file_path); //推断file_path是否为文件夹
boost::filesystem::recursive_directory_iterator beg_iter(file_path);
boost::filesystem::recursive_directory_iterator end_iter;
for (; beg_iter != end_iter; ++beg_iter)
{
if (boost::filesystem::is_directory(*beg_iter))
{
continue;
}
else
{
std::string strPath = beg_iter->path().string(); //遍历出来的文件名称
int x = ;
}
}
boost::filesystem::path new_file_path = file_path / "test.txt";
if (boost::filesystem::is_regular_file(new_file_path)) //推断是否为普通文件
{
UINT sizefile = boost::filesystem::file_size(new_file_path); //文件大小(字节)
int x = ;
}
boost::filesystem::remove(new_file_path);//删除文件new_file_path
} // recusively copy file or directory from $src to $dst
void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst)
{
if (!boost::filesystem::exists(dst))
{
boost::filesystem::create_directories(dst);
}
for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it)
{
const boost::filesystem::path newSrc = src / it->path();
const boost::filesystem::path newDst = dst / it->path();
if (boost::filesystem::is_directory(newSrc))
{
CopyFiles(newSrc, newDst);
}
else if (boost::filesystem::is_regular_file(newSrc))
{
boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
}
else
{
_ftprintf(stderr, _T("Error: unrecognized file - %s"), newSrc.string().c_str());
}
}
} bool CopyDirectory(const std::string &strSourceDir, const std::string &strDestDir)
{
boost::filesystem::recursive_directory_iterator end; //设置遍历结束标志,用recursive_directory_iterator即可循环的遍历目录
boost::system::error_code ec;
for (boost::filesystem::recursive_directory_iterator pos(strSourceDir); pos != end; ++pos)
{
//过滤掉目录和子目录为空的情况
if (boost::filesystem::is_directory(*pos))
continue;
std::string strAppPath = boost::filesystem::path(*pos).string();
std::string strRestorePath;
//replace_first_copy在algorithm/string头文件中,在strAppPath中查找strSourceDir字符串,找到则用strDestDir替换,替换后的字符串保存在一个输出迭代器中
boost::algorithm::replace_first_copy(std::back_inserter(strRestorePath), strAppPath, strSourceDir, strDestDir);
if (!boost::filesystem::exists(boost::filesystem::path(strRestorePath).parent_path()))
{
boost::filesystem::create_directories(boost::filesystem::path(strRestorePath).parent_path(), ec);
}
boost::filesystem::copy_file(strAppPath, strRestorePath, boost::filesystem::copy_option::overwrite_if_exists, ec);
}
if (ec)
{
return false;
}
return true;
}

最新文章

  1. wamp 修改默认apache 80端口
  2. bootstrap插件学习-bootstrap.collapse.js
  3. 配置Tomcat出现Unsupported major.minor version 51.0
  4. 第33篇 js 常用简单的写法
  5. 分布式唯一id:snowflake算法思考
  6. 微信小程序基础之创建使用教程
  7. SQL Server Cast、Convert数据类型转换
  8. 关于信号打断正在读取终端的read与select来监视0文件描述符的问题
  9. sublime text 入门
  10. PHP与.Net的区别(一)接口
  11. BOM心得
  12. PI monitor error process-RESOURCE_NOT_FOUND-转
  13. pdf修复
  14. RedHat7.0更新yum源
  15. 面向对象进阶------>模块 json pickle hashlib
  16. Linux发行版本应用场景
  17. 绝妙的SQL行列转换语句
  18. CSS 绝对居中方案
  19. Java学习笔记(16)
  20. iOS开发之--制作属于自己的frameWork

热门文章

  1. phpcms2008网站漏洞如何修复 远程代码写入缓存漏洞利用
  2. unity开发c#代码
  3. 安装 Node.js v8.0 生产环境
  4. 基于vue来开发一个仿饿了么的外卖商城(一)
  5. java入门---windows和Linux,UNIX,Solaris,FreeBSD下开发环境配置
  6. EF报错“EntityValidationErrors”
  7. 『AngularJS』Service
  8. 51单片机实现定时器中断0-F
  9. Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增
  10. UVA 11881 Internal Rate of Return(数学+二分)