博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
删除指定目录下所有文件及目录
阅读量:4044 次
发布时间:2019-05-24

本文共 1443 字,大约阅读时间需要 4 分钟。

2014年3月24日18:04:58

 删除指定目录下所有文件及目录

1、使用VS2008创建一个带预编译头的控制台项目。

2、源文件内容:

#include "stdafx.h" #include 
#include
using namespace std;// 删除指定目录下所有文件及目录 bool DelDirContent(TCHAR * tcsPath) { WIN32_FIND_DATA wfd; HANDLE hFind; TCHAR tcsFullPath[MAX_PATH] = {0}; TCHAR tcsDirFilter[MAX_PATH] = {0}; _tcscpy(tcsDirFilter,tcsPath); _tcscat(tcsDirFilter,_T("\\*")); hFind = FindFirstFile(tcsDirFilter, &wfd); if (hFind == INVALID_HANDLE_VALUE) { printf ("FindFirstFile failed (%d)\n", GetLastError()); return false; } do { if (_tcscmp(wfd.cFileName, _T(".")) == 0 || _tcscmp(wfd.cFileName, _T("..")) == 0 ) { continue; } _tcscpy(tcsFullPath,tcsPath); _tcscat(tcsFullPath,_T("\\")); _tcscat(tcsFullPath,wfd.cFileName); //去掉只读属性 DWORD dwAttributes = GetFileAttributes(tcsFullPath); if (dwAttributes & FILE_ATTRIBUTE_READONLY) { dwAttributes &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(tcsFullPath, dwAttributes); } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { DelDirContent(tcsFullPath); RemoveDirectory(tcsFullPath); } else { if(!DeleteFile(tcsFullPath)) { printf("删除文件失败\n"); } } }while (FindNextFile(hFind, &wfd)); FindClose(hFind); return true; } int main( void ) { char ch; TCHAR tcsFileName[MAX_PATH] = {0}; _tcscpy(tcsFileName,TEXT("CenterTransLog")); DelDirContent(tcsFileName); printf("程序结束\n"); cin>>ch; return 0; }
 

转载地址:http://kjwci.baihongyu.com/

你可能感兴趣的文章
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>