VC删除文件夹操作代码

VS删除文件夹

//功能: 删除非空目录文件夹

//输入: DirName 文件夹名

//输出: 删除是否成功

//功能: 迭代文件夹内文件,一个一个删除

BOOL DeleteDirectory(char *DirName)

{

CFileFind tempFind;

char tempFileFind[200];

sprintf(tempFileFind,"%s\.",DirName);

BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);

while(IsFinded)

{

IsFinded=(BOOL)tempFind.FindNextFile();

if(!tempFind.IsDots())

{

char foundFileName[200];

strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));

if(tempFind.IsDirectory())

{

char tempDir[200];

sprintf(tempDir,"%s\%s",DirName,foundFileName);

DeleteDirectory(tempDir);

}

else

{

char tempFileName[200];

sprintf(tempFileName,"%s\%s",DirName,foundFileName);

DeleteFile(tempFileName);

}

}

}

tempFind.Close();

if(!RemoveDirectory(DirName))

{

MessageBox("删除目录失败!","警告信息",MB_OK);

return FALSE;

}

return TRUE;

}

---------------------------------------------------------记录

void CDeletefileDlg::OnBtnok() //单一文件选择框

{

// TODO: Add your control notification handler code here

CFileDialog file(true);

CString str;

if (IDOK==file.DoModal())

{

// str=file.GetFileName();

str=file.GetPathName();

}

SetDlgItemText(IDC_FILENAME,str);

}

void CDeletefileDlg::OnBtndelete() //删除单一文件

{

// TODO: Add your control notification handler code here

CString cstr;

GetDlgItemText(IDC_FILENAME,cstr);

CFile::Remove(cstr);

SetDlgItemText(IDC_FILENAME,"");

MessageBox(cstr+"已删除成功!");

}

void CDeletefileDlg::OnBtndirtest() //文件夹选择框

{

// TODO: Add your control notification handler code here

BROWSEINFO bi; //定义文件浏览对象

char buffer[MAX_PATH]; //定义字符缓冲区

ZeroMemory(buffer,MAX_PATH); //初始化字符缓冲区

bi.hwndOwner=GetSafeHwnd();

bi.pidlRoot=NULL;

bi.pszDisplayName=buffer;

bi.lpszTitle="请选择一个文件夹"; //设置浏览框标题

bi.ulFlags=BIF_EDITBOX;

bi.lpfn=NULL;

bi.lParam=0;

bi.iImage=0;

LPITEMIDLIST pList=NULL;

if ( (pList=SHBrowseForFolder(&bi)) != NULL ) //弹出浏览框对话框

{

char path[MAX_PATH];

ZeroMemory(path,MAX_PATH);

SHGetPathFromIDList(pList,path); //获取用户目录

GetDlgItem(IDC_DIR)->SetWindowText(path);

}

}

void CDeletefileDlg::OnBtndel() //目录删除函数

{

// TODO: Add your control notification handler code here

GetDlgItemText(IDC_DIR,filestr);

delDirFile(filestr); //删除目录下的所有文件

SetDlgItemText(IDC_DIR,"");

RemoveDirectory(filestr); //删除空目录

MessageBox(filestr+"目录已经完全删除!");

}

void CDeletefileDlg::delDirFile(CString spath) //删除目录下的所有文件函数

{

CFileFind find;

BOOL bworking=find.FindFile(spath+"\.");

while (bworking)

{

bworking=find.FindNextFile();

CString filename=find.GetFileName();

if (find.MatchesMask(FILE_ATTRIBUTE_DIRECTORY))

{

if ( (filename!=".") && (filename != "..") ) //滤过 . 和 .. 文件夹

{

delDirFile(spath+"\"+filename);

}

}

else

{

CFile::Remove(spath+"\"+filename); //如果不是文件夹 直接删除

}

RemoveDirectory(spath+"\"+filename); //

}

}

void CDeletefileDlg::finddir(CString findpath) //查找根目录下的所有子文件

{

CString deldir;

GetDlgItemText(IDC_DELDIRNAME,deldir);

CFileFind find;

CString filename,tempstr,filepath;

BOOL bworking=find.FindFile(findpath+"\.");

while(bworking)

{

bworking=find.FindNextFile();

filepath=find.GetFilePath();

filename=find.GetFileName();

tempstr=filename.Right(5);

if ((deldir==tempstr) && find.MatchesMask(FILE_ATTRIBUTE_DIRECTORY))

{

delDirFile(filepath);

RemoveDirectory(filepath);

}

else if( (filename!=".") && (filename != "..") && find.MatchesMask(FILE_ATTRIBUTE_DIRECTORY) ) //这里的递归有问题

{

finddir(findpath+"\"+filename);

}

}

find.Close();

}

void CDeletefileDlg::OnDeldebug() //删除特定的文件夹函数

{

// TODO: Add your control notification handler code here

CString path;

GetDlgItemText(IDC_DIR,path);

CString childpath;

GetDlgItemText(IDC_DELDIRNAME,childpath);

finddir(path);

SetDlgItemText(IDC_DIR,"");

SetDlgItemText(IDC_DELDIRNAME,"");

MessageBox(path+"下的所有"+childpath+"已经全部删除!");

}

----------------------------------------记录2

//删除文件夹目录(非空)

bool 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;

}

/**//////////////////////////////////////////

//下面是应用,CString m_strDir 是一个文件夹路径,如:d:downloadpic

BOOL DelAll()

...{

if(PathFileExists(m_strDir))

DeleteDirectory((LPSTR)(LPCTSTR)m_strDir);

return 1;

}

猜你喜欢

转载自blog.51cto.com/14226273/2380210