//删除文件夹
SHFILEOPSTRUCT FileOp
FileOp.fFlags = FOF_NOCONFIRMATION
FileOp.hNameMappings = NULL
FileOp.hwnd = NULL
FileOp.lpszProgressTitle = NULL
FileOp.pFrom = "要删除的目录"
FileOp.pTo = NULL
FileOp.wFunc = FO_DELETE
SHFileOperation(&FileOp)
2、删除文件夹中所有文件的方法如下:
BOOL CDlgData::DeleteDirectory(char *sDirName){
CFileFind tempFind
char sTempFileFind[200]
sprintf(sTempFileFind,"%s\\*.*",sDirName)
BOOL IsFinded = tempFind.FindFile(sTempFileFind)
while (IsFinded)
{
IsFinded = tempFind.FindNextFile()
if (!tempFind.IsDots())
{
char sFoundFileName[200]
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200))
if (tempFind.IsDirectory())
{
char sTempDir[200]
sprintf(sTempDir,"%s\\%s",sDirName,sFoundFileName)
DeleteDirectory(sTempDir)
}
else
{
char sTempFileName[200]
sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName)
DeleteFile(sTempFileName)
}
}
}
tempFind.Close()
if(!RemoveDirectory(sDirName))
{
return FALSE
}
return TRUE
}
find(char * lpPath){
char szFind[MAX_PATH]
WIN32_FIND_DATA FindFileData
strcpy(szFind,lpPath)
strcat(szFind,"*.*")
HANDLE hFind=::FindFirstFile(szFind,&FindFileData)
if(INVALID_HANDLE_VALUE == hFind) return
while(TRUE)
{
if(FindFileData.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
if(FindFileData.cFileName[0]!='.')
{
strcpy(szFile,lpPath)
strcat(szFile,"")
strcat(szFile,FindFileData.cFileName)
find(szFile)
}
}
else
{
cout <<FindFileData.cFileName
}
if(!FindNextFile(hFind,&FindFileData)) break
}
FindClose(hFind)
}
#include <afxwin.h>
#include <iostream>
using namespace std
void Recurse(LPCTSTR pstr)
{
CFileFind finder
// build a string with wildcards
CString strWildcard(pstr)
strWildcard += _T("\\*.*")
// start working for files
BOOL bWorking = finder.FindFile(strWildcard)
while (bWorking)
{
bWorking = finder.FindNextFile()
// skip . and .. filesotherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue
CString sFileName = finder.GetFileName()
cout <<(LPCTSTR)sFileName <<endl//输出查找文件夹下的所有文件名
}
finder.Close()
}
int main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))//初始化MFC
cout <<"panic!" <<endl
else
Recurse(_T("C:"))
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)