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;
}

boost 文件操作的更多相关文章

  1. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  2. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  3. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  4. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  5. SQL Server附加数据库报错:无法打开物理文件,操作系统错误5

    问题描述:      附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  8. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

  9. Python的文件操作

    文件操作,顾名思义,就是对磁盘上已经存在的文件进行各种操作,文本文件就是读和写. 1. 文件的操作流程 (1)打开文件,得到文件句柄并赋值给一个变量 (2)通过句柄对文件进行操作 (3)关闭文件 现有 ...

随机推荐

  1. CONCATENATE命令(文字列の結合)

    CONCATENATE命令とは文字列の結合を行う命令である.文字列を扱うChar, Numeric, Dats, Time, Stringの変数で使用する事が可能だ.単純に文字列の結合のみを行う方法. ...

  2. JS 实现随机验证码功能

    1.验证码 验证是网页常出现的一个验证点,所谓验证码类型有很多,下面代码只是实现一个简单的验证功能. <div> <input type = "text" id ...

  3. 【转】Django添加静态文件设置

    STATIC_URL = '/statics/'STATIC_ROOT= os.path.join(BASE_DIR, 'statics')STATICFILES_DIRS = ( os.path.j ...

  4. samba与apache配置使用

    samba与apache根目录 1.直接将apache用户作为samba用户 2.给apache用户赋宇网站根目录的acl rwx权限 #注意 第一次不要加默认的权限 setfacl -m u:apa ...

  5. nginx https ssl 配置

    #设置https 访问server { listen ; server_name www.xxx.com; access_log xxx/xxx/xxx.log combined; index ind ...

  6. c++ combination by next_permutation

    #include <iostream> #include <algorithm> #include <vector> int main() { int n, r; ...

  7. 自动化测试--testNG

    该文章主要介绍 testNG(testing next generation,下一代测试技术)框架的使用. 1.首先安装testNG 2.安装完成后,创建maven项目,导入TESTNG和seleni ...

  8. 输出1-n的全排(递归C++)

    [问题描述] 输出1到n之间所有不重复的排列,即1到n的全排,要求所产生的任一数列不含有重复的数字. [代码展示] #include<iostream>using namespace st ...

  9. 辨析ADK&JVM&JRE&JDK&ADT

    一.SDK 英文全称:Software Development Kit 中文译名:软件开发工具包 详解: 由第三方服务商提供的实现软件产品某项功能的工具包. 为了扩展软件功能或其它方面而设计出来给开发 ...

  10. Linux IO乱序

    原创翻译,转载请注明出处. 在一些平台,所谓的内存映射I/O在保序执行这方面是没有保障的.在这些平台,驱动写入器负责保证I/O写操作按照预期的顺序写到设备内存映射地址. 代表性的做法是通过读取一个安全 ...