間隔刪除文件小工具

工具界面如下,實現功能爲一定的間隔刪除或者移動文件。

核心代碼如下:

	// TODO:  在此添加命令處理程序代碼
#ifdef NEED_DIR
	CString strDir;
	GetDlgItemText(IDC_EDIT_DIR, strDir);

	if (!strDir.GetLength())
	{
		strDir = CMfcStrFile::BrowseDir();
		if (strDir.GetLength() > 0)
		{
			SetDlgItemText(IDC_EDIT_DIR, strDir);
		}
		else
		{
			return;
		}
	}
	_tstring sDir = CMfcStrFile::CString2string(strDir);
	m_cfg.vDirPaths.clear();
	m_cfg.vDirPaths.push_back(sDir);
#endif // NEED_DIR

#ifdef NEED_FILE
	CString strFile;
	GetDlgItemText(IDC_EDIT_FILE, strFile);
	if (!strFile.GetLength())
	{
		strFile = CMfcStrFile::OpenFile();
		if (strFile.GetLength() > 0)
		{
			SetDlgItemText(IDC_EDIT_FILE, strFile);
		}
		else
		{
			return;
		}
	}
	_tstring sFile = CMfcStrFile::CString2string(strFile);
	m_cfg.vFilePaths.clear();
	m_cfg.vFilePaths.push_back(sFile);
#endif // NEED_FILE

	//開始顯示進度
	CTaskBarProgress tbp(m_hWnd);
	CProgressInterface* ppi = &tbp;
	ppi->Start();
	CElapsedTime et;
	
	//記錄日誌
	CLOG::Out("start task!");
	//記錄耗時
	et.Begin();

	/*********************************這裏增加主程序 開始***************************************/
	CWnd* pWndFirstIgnore = GetDlgItem(IDC_COMBO_FIRSTIGNORE);
	if (pWndFirstIgnore)
	{
		CComboBox* pComBox = (CComboBox*)pWndFirstIgnore;
		CString strFirstIgnore;
		pComBox->GetWindowText(strFirstIgnore);
		CStdTpl::ConvertFromString(m_cfg.nFirstIgnore, CMfcStrFile::CString2string(strFirstIgnore));
		if (m_cfg.nFirstIgnore < 0)
		{
			m_cfg.nFirstIgnore = 0;
			pComBox->SetCurSel(0);
		}
	}

	CWnd* pWndGapDelete = GetDlgItem(IDC_COMBO_GAPDELETENUM);
	if (pWndGapDelete)
	{
		CComboBox* pComBox = (CComboBox*)pWndGapDelete;
		CString strGapDelete;
		pComBox->GetWindowText(strGapDelete);
		CStdTpl::ConvertFromString(m_cfg.nGapDelete, CMfcStrFile::CString2string(strGapDelete));
		if (m_cfg.nGapDelete < 0)
		{
			m_cfg.nGapDelete = 0;
			pComBox->SetCurSel(0);
		}
	}

	m_cfg.bBakFiles = ((CButton*)GetDlgItem(IDC_CHECK_BAK))->GetCheck() == TRUE;

	std::vector<_tstring> vFiles;
	size_t nFileNum = getFiles(sDir, vFiles, m_cfg.vSuffixs, false);

	_tstring strBakDir = CStdStr::AddSlashIfNeeded(sDir) + _T("bak");
	if (m_cfg.bBakFiles && !CStdDir::IfAccessDir(strBakDir) && !CStdDir::CreateDir(strBakDir))
	{
		return;
	}
	int nPlus = m_cfg.nGapDelete - m_cfg.nFirstIgnore % m_cfg.nGapDelete;
	if (m_cfg.bBakFiles)
	{
		for (size_t i = m_cfg.nFirstIgnore; i < nFileNum;)
		{
			if ((i + nPlus) % m_cfg.nGapDelete == 0)
			{
				for (size_t j = i; j < i + m_cfg.nGapDelete && j < nFileNum; ++j)
				{
					std::string strCur = CStdStr::ws2s(vFiles[j]);
					std::string strNewPath = CStdStr::ws2s(CStdStr::AddSlashIfNeeded(strBakDir) + CStdStr::GetNameOfFile(vFiles[j]));
					rename(strCur.c_str(), strNewPath.c_str());
				}
				i += 2 * m_cfg.nGapDelete;
			}
			else
			{
				return;
			}
		}
	}
	else
	{
		for (size_t i = m_cfg.nFirstIgnore; i < nFileNum;)
		{
			if ((i + nPlus) % m_cfg.nGapDelete == 0)
			{
				for (size_t j = i; j < i + m_cfg.nGapDelete && j < nFileNum; ++j)
				{
					std::string strCur = CStdStr::ws2s(vFiles[j]);
					remove(strCur.c_str());
				}
				i += 2 * m_cfg.nGapDelete;
			}
			else
			{
				return;
			}
		}
	}

	/*********************************這裏增加主程序 結束***************************************/

	//結束耗時
	int nMin = 0, nSecond = 0, nMilliSecond = 0;
	et.End(nMin, nSecond, nMilliSecond);
	//結束日誌
	CLOG::Out("end task!");
	CLOG::Out(_T("This task costs %d min %d second %d millisecond!"), nMin, nSecond, nMilliSecond);
	CLOG::End();

	//結束進度顯示
	ppi->End();
	FlashWindow(TRUE);
	WriteIniFile(GetIniPath(), m_cfg);

	CString strTips;
	strTips.Format(_T("本次耗時 %d分%d秒%d毫秒!"), nMin, nSecond, nMilliSecond);
	AfxMessageBox(strTips);

	CDialogEx::OnOK();

更多的交流,歡迎加入留言。

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