/*判断一个路径是否是已存在的目录*/
bool IsDirectory(const std::wstring& pstrPath)
{
DWORD dw = GetFileAttributes(pstrPath.c_str());
if (dw == INVALID_FILE_ATTRIBUTES)
{
return false;
}
return (dw & FILE_ATTRIBUTE_DIRECTORY) != 0;
} /*复制目录及目录中的所有内容*/
bool CopyFolder(const std::wstring& pstrFolder, const std::wstring& pstrDest)
{
/*检查输入目录是否是合法目录*/
if (!IsDirectory(pstrFolder))
{
return false;
}
if (!IsDirectory(pstrDest))
{
CreateDirectoryW(pstrDest.c_str(), NULL);
} std::wstring strFind = pstrFolder;
if (*strFind.rbegin() != L'\\' &&
*strFind.rbegin() != L'/')
{
strFind.append(L"\\");
}
strFind.append(L"*.*");
std::wstring strDest = pstrDest;
if (*strDest.rbegin() != L'\\' &&
*strDest.rbegin() != L'/')
{
strDest.append(L"\\");
} /*打开文件查找,查看源目录中是否存在匹配的文件*/
/*调用FindFile后,必须调用FindNextFile才能获得查找文件的信息*/
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFileW(strFind.c_str(), &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
do
{
std::wstring strSubFolder;
std::wstring strDestFolder;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (wfd.cFileName[0] == L'.')
{
continue;
}
else
{
strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
strDestFolder = strDest + +wfd.cFileName;
CopyFolder(strSubFolder, strDestFolder);
}
}
else
{
strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
strDestFolder = strDest + +wfd.cFileName;
CopyFileW(strSubFolder.c_str(), strDestFolder.c_str(), FALSE);
}
} while (FindNextFileW(hFind, &wfd)); /*删除空目录*/
FindClose(hFind);
return true;
} /*删除目录及目录中的所有内容*/
bool DeleteFolder(const std::wstring& pstrFolder, bool recursive)
{
/*检查输入目录是否是合法目录*/
if (!IsDirectory(pstrFolder))
{
return false;
} std::wstring strFind = pstrFolder;
if (*strFind.rbegin() != L'\\' &&
*strFind.rbegin() != L'/')
{
strFind.append(L"\\");
}
strFind.append(L"*.*"); /*打开文件查找,查看源目录中是否存在匹配的文件*/
/*调用FindFile后,必须调用FindNextFile才能获得查找文件的信息*/
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFileW(strFind.c_str(), &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
do
{
std::wstring strSubFolder;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (wfd.cFileName[0] == L'.')
{
continue;
}
else if (recursive)
{
strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
DeleteFolder(strSubFolder, recursive);
}
}
else
{
strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
DeleteFileW(strSubFolder.c_str());
}
} while (FindNextFileW(hFind, &wfd)); /*删除空目录*/
FindClose(hFind);
return RemoveDirectoryW(pstrFolder.c_str()) == TRUE;
}

  

递归拷贝目录与删除目录 WindowsAPI C++的更多相关文章

  1. Linux下拷贝目录和删除

    cp命令用于复制文件或目录,若同事指定两个以上的文件或目录,切最后一个目的地是一个已存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录,而最后的目的地并非一个已存在的 ...

  2. 原生Java代码拷贝目录

    拷贝.移动文件(夹),有三方包commons-io可以用,但是有时候有自己的需求,只能使用原生java代码,这时可以用以下几种方式进行拷贝: 1.使用系统命令(Linux)调用 此种方式对操作系统有要 ...

  3. PHP-递归扫描目录和删除目录

    (1) 通过递归扫描目录并打印 // php递归扫描目录 function scanMyDir($path){ // 打开目录 $dh = opendir($path); echo '<ul&g ...

  4. php递归操作目录 递归对参数转义

    header("Content-type:text/html;charset=utf-8"); //递归读取目录 function reddir($path,$level=0) { ...

  5. linux cp -r chmod -R 递归拷贝 删除 改权限

    在linux下拷贝的时候有时候会出现cp:omitting directory的错误 ,例如 cp:omitting directory "bbs" 说明bbs目录下面还有目录,不 ...

  6. python 生成、删除、拷贝目录

    1. 生成目录 函数原型:distutils.dir_util.mkpath(name[, mode=0777, verbose=0, dry_run=0]) from distutils impor ...

  7. C# 拷贝目录

    public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name ...

  8. (实用篇)PHP不用递归遍历目录下所有文件的代码

    <?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...

  9. Java递归列出目录下全部文件

    Java递归列出目录下全部文件 /** * 列出指定目录的全部内容 * */ import java.io.*; class hello{ public static void main(String ...

随机推荐

  1. VUE: 移动端长按弹出确认删除地址(2)

    之前有一篇文章也写了长按弹出确认框的功能,在android机上测试过完全没问题,到后面整体测试时发现IOS这个功能长按移除就消失了, 除非长按不松手,用另外一只手点击确定才能完成操作,所以这次做了修改 ...

  2. jquery button选择器 语法

    jquery button选择器 语法 作用::button 选择器选取类型为 button 的 <button> 元素和 <input> 元素.大理石平台价格表 语法:$(& ...

  3. hadoop安装后运行一个单实例(测试MapReduce程序)

    1.安装hadoop 解压hadoop-1.2.1-bin.tar.gz包   tar -zxvf hadoop-1.2.1-bin.tar.gz  /opt/modules/ 解压后在/opt/mo ...

  4. word粘贴图片到ekitor

    最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...

  5. luoguP3371 【模板】单源最短路径

    P3371 [模板]单源最短路径 3K通过 10.7K提交 题目提供者 HansBug 标签 云端↑ 难度 普及/提高- 时空限制 1s / 128MB 题目描述 如题,给出一个有向图,请输出从某一点 ...

  6. AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)

    题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...

  7. Miller Robin大素数判定

    Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...

  8. LeetCode---Sort && Segment Tree && Greedy

    307. Range Sum Query - Mutable 思路:利用线段树,注意数据结构的设计以及建树过程利用线段树,注意数据结构的设计以及建树过程 public class NumArray { ...

  9. TCP->IP输出 之 ip_queue_xmit、ip_build_and_send_pkt、ip_send_unicast_reply

    概述 ip_queue_xmit是ip层提供给tcp层发送回调,大多数tcp发送都会使用这个回调,tcp层使用tcp_transmit_skb封装了tcp头之后,调用该函数,该函数提供了路由查找校验. ...

  10. Nginx-rtmp点播之业务流程分析

    1. 点播的播放流程分析 1.1 ngx_rtmp_cycle 在握手结束后,即进入该函数中做进一步处理. void ngx_rtmp_cycle(ngx_rtmp_session_t *s) { n ...