Windows API函数删除指定文件目录下所有内容vc6.0通过

22 篇文章 0 订阅
// FileOpt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <string>
#include <WINDOWS.H>
#include <TCHAR.H>
using namespace std;


// 删除指定目录下所有文件及目录
BOOL DelDirFileOpt(string szPath)
{
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	string sFullPath;
	string sFindFilter;
	DWORD dwAttributes = 0;
	
	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}
	
	do
	{
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 )
		{
			continue;
		}
		
		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;
		
		//去掉只读属性
		dwAttributes = GetFileAttributes(sFullPath.c_str());
		if (dwAttributes & FILE_ATTRIBUTE_READONLY)
		{
			dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
			SetFileAttributes(sFullPath.c_str(), dwAttributes);
		}
		
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			printf("进入目录%s\n",sFullPath.c_str());
			DelDirFileOpt(sFullPath.c_str());
			RemoveDirectory(sFullPath.c_str());
			printf("删除目录%s成功\n",sFullPath.c_str());
		}
		else
		{
			if ( _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
			{
				//WipeFile(szPath, wfd.cFileName);
			}
			DeleteFile(sFullPath.c_str());
			printf("文件%s删除成功\n",sFullPath.c_str());
		}
	}while (FindNextFile(hFind, &wfd));
	
	FindClose(hFind);
	
	return TRUE;
}

#define MAX_NUM 262

int main(int argc, char* argv[])
{
	char cCurPath[MAX_NUM] = {0};

	GetCurrentDirectory(MAX_NUM,cCurPath);

	string strDelPath = (string)cCurPath + (string)"\\testDir";

	DelDirFileOpt(strDelPath.c_str());

	

	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值