刪除指定文件夾下小於指定大小的全部文件

<img src="https://img-blog.csdn.net/20141009142553796?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVpaGVpMzY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

</pre><pre class="cpp" name="code">void CdelsmallfileDlg::OnBnClickedButtonSetsize()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	std::string strTemp = m_strSize.GetBuffer(m_strSize.GetLength());
	m_strSize.ReleaseBuffer(m_strSize.GetLength());
	m_nFileLength = atoi(strTemp.c_str());
}


void CdelsmallfileDlg::OnBnClickedButtonSelectpath()
{
	// TODO: Add your control notification handler code here

	CString sFolderPath;  
	BROWSEINFO bi;  
	TCHAR Buffer[MAX_PATH];  
	//初始化入口參數bi開始  
	bi.hwndOwner = NULL;  
	bi.pidlRoot =NULL;//初始化制定的root目錄很不容易,  
	bi.pszDisplayName = Buffer;//此參數如爲NULL則不能顯示對話框  
	bi.lpszTitle = _T("選擇路徑");  
	//bi.ulFlags = BIF_BROWSEINCLUDEFILES;//包括文件  
	bi.ulFlags = BIF_EDITBOX;//包括文件  
	bi.lpfn = NULL;  
	bi.iImage=IDR_MAINFRAME;  
	//初始化入口參數bi結束  
	LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//調用顯示選擇對話框  
	if(pIDList)  
	{  
		SHGetPathFromIDList(pIDList, Buffer);  
		//取得文件夾路徑到Buffer裏  
		m_strPath = Buffer;//將路徑保存在一個CString對象裏
		GetDlgItem(IDC_EDIT_PATH)->SetWindowText(m_strPath);
	}  

}

void CdelsmallfileDlg::TravelFolder(CString strDir)
{
    CFileFind filefind;                             //聲明CFileFind類型變量
    CString strWildpath = strDir + _T("//*.*");     //所有文件都列出。
    if(filefind.FindFile(strWildpath, 0))                    //開始檢索文件
    {
        BOOL bRet = TRUE;
        while(bRet)
        {
            bRet = filefind.FindNextFile();                 //枚舉一個文件
            if(filefind.IsDots())                                 //如果是. 或 .. 做下一個
				continue;

            if(!filefind.IsDirectory())                          //不是子目錄,把文件名打印出來
            {
                CString strTextOut = strDir + CString(_T("//")) + filefind.GetFileName();
                //TRACE(_T("file = %s/r/n"), strTextOut);
				int nFileLength = filefind.GetLength();
				if ( nFileLength < m_nFileLength )
				{
					DeleteFile(strTextOut);
					m_EditFileName.SetWindowText( _T("Delete File ") + strTextOut + _T("...") );
				}
            }
            else                                                    //如果是子目錄,遞歸調用該函數
            {
                CString strTextOut = strDir + CString(_T("//")) + filefind.GetFileName();
                TRACE(_T("dir = %s/r/n"), strTextOut);
                TravelFolder(strTextOut);//遞歸調用該函數打印子目錄裏的文件
            }
        }
        filefind.Close();
    }
}

void CdelsmallfileDlg::OnBnClickedButtonRun()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	std::string strTemp = m_strSize.GetBuffer(m_strSize.GetLength());
	m_strSize.ReleaseBuffer(m_strSize.GetLength());
	m_nFileLength = atoi(strTemp.c_str());
	m_nFileLength *= 1024;
	GetDlgItem(IDC_EDIT_PATH)->GetWindowText(m_strPath);

	TravelFolder(m_strPath);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章