/*判断一个路径是否是已存在的目录*/
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. Blade 模板

    在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变量.该变量是一个stdClass实例,包含了当前循环的元数据信息,让 ...

  2. 51 Nod 1092 回文字符串

    1092 回文字符串  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每 ...

  3. 第七届蓝桥杯试题c/c++A组方格填数 回溯法

    方格填数如下的10个格子   +--+--+--+   |  |  |  |+--+--+--+--+|  |  |  |  |+--+--+--+--+|  |  |  |+--+--+--+(如果 ...

  4. Codevs 4019 想越狱的小明

    4019 想越狱的小明 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题目描述 Description 这次小明来到了经典美剧<越狱>的场景里-- 它 ...

  5. 日照学习提高班day4测试

    A 思路: 一看到这个题,他不仅要求输出字典序最小的串,还要满足两两不重复,所以我们可以先输出ababab...什么的,最后缀上要求的k-2种字母 坑点: 当然这样想是不完全的!该题是拥有许多特殊情况 ...

  6. python 多线程实现循环打印 abc

    python 多线程实现循环打印 abc 好久没写过python了, 想自己实践一下把 非阻塞版 import threading import time def print_a(): global ...

  7. POJ1990--POJ 1990 MooFest(树状数组)

    Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, ...

  8. 20.Python类型转换,Python数据类型转换函数大全

    虽然 Python 是弱类型编程语言,不需要像 Java 或 C 语言那样还要在使用变量前声明变量的类型,但在一些特定场景中,仍然需要用到类型转换. 比如说,我们想通过使用 print() 函数输出信 ...

  9. truncate at 255 characters with xlsx files(OLEDB方式读取Excel丢失数据、字符串截断的原因和解决方法)

    The TypeGuessRows setting is supported by ACE. Note the version numbers in the key may change depend ...

  10. Java - 自动装箱与拆箱详解

    1.装箱与拆箱 装箱,将基本数据类型转为包装类型.拆箱,将包装类型转为基本数据类型. // Byte, Short, Integer, Long, Double, Float, Boolean, Ch ...