1. 程式人生 > >VC刪除文件夾操作代碼

VC刪除文件夾操作代碼

刪除文件 child tst att rfi strcpy 設置 spl 文件夾路徑

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;

}

VC刪除文件夾操作代碼