/*判断一个路径是否是已存在的目录*/
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. .py文件打包成.exe文件

    # 使用pyinstaller模块 # pip install pyinstaller # 在命令行执行 pyinstaller -F xxx.py

  2. yolov3 in PyTorch

    https://github.com/ultralytics/yolov3 Introduction简介 This directory contains PyTorch YOLOv3 software ...

  3. Keras学习笔记二:保存本地模型和调用本地模型

    使用深度学习模型时当然希望可以保存下训练好的模型,需要的时候直接调用,不再重新训练 一.保存模型到本地 以mnist数据集下的AutoEncoder 去噪为例.添加: file_path=" ...

  4. 异常处理(Exception Handling)

    java里的异常处理(Exception)Exception 是在程序执行过程中发生的一些不希望发生的事情,这些事情如果不被好好处理,就会导致奇怪的结果或者是程序终结.Exception Handle ...

  5. 20165213 Exp 8 Web基础

    Exp 8 Web基础 一.基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器 ...

  6. Mac安装软件提示文件损坏

    Mac安装软件提示文件损坏,请移至废纸篓 sudo spctl --master-disable

  7. Fastadmin 写关联命名时,最好前后台用同一个model,方便管理(会出现命名空间问题)

    1.php think crud -t test --relation=category(外键表1) --relation=admin(外键表2) --relationforeignkey=categ ...

  8. Ubuntu16.04下安装最新版本的CMake

      当前最新版CMake为3.9.1.. Ubuntu中更新cmake到最新版本,过程如下: 1. 卸载已经安装的旧版的CMake[非必需] apt-get autoremove cmake 2. 文 ...

  9. JDK压缩指针

    https://www.cnblogs.com/iceAeterNa/p/4877549.html

  10. LC 646. Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...