// RecursionDelete.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" //#include
#include <afx.h> #include <iostream> using namespace std; //void DeleteDir(CString szPath);
void RecursionDelete(CString szPath); int _tmain(int argc, _TCHAR* argv[]) { CString Dirname = "D:\\test"; //DeleteDir("D:\\test");
RecursionDelete(Dirname); getchar(); return 0; } void RecursionDelete(CString szPath) { CFileFind ff; CString strPath = szPath; //look for all files
if (strPath.Right(1) != "\\") { strPath += "\\"; } strPath += "*.*"; BOOL bRet; if (ff.FindFile(strPath)) { do { bRet = ff.FindNextFile(); if (ff.IsDots()) { continue; } strPath = ff.GetFilePath(); if (!ff.IsDirectory()) { //delete the file
::SetFileAttributes(strPath, FILE_ATTRIBUTE_NORMAL); ::DeleteFile(strPath); } else { RecursionDelete(strPath); //delete the directory
::SetFileAttributes(strPath, FILE_ATTRIBUTE_NORMAL); ::RemoveDirectory(strPath); } } while (bRet); } ::SetFileAttributes(szPath, FILE_ATTRIBUTE_NORMAL); /* 如果没有这句话,最根的那个目录不能删除(即main里只的test不能被删除),如果加上这句话就可以删除。 为什么这样,还没有弄明白,还望高手指教 */ ff.Close(); ::RemoveDirectory(szPath); }
|
这里在编译的时候可能有问题,因为用到了MFC类库,所以要进行设置(vs08)
project -> properties -> project defaults ->修改use of mfc 为Use MFC in a Shared DLL
阅读(832) | 评论(0) | 转发(0) |