文件清單小工具

將某驅動器或者文件夾內指定類型的文件列出來。

界面如圖:

核心代碼如下:

	// 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);
	_conf.vDirPaths.clear();
	_conf.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);
	_conf.vFilePaths.clear();
	_conf.vFilePaths.push_back(sFile);
#endif // NEED_FILE

	//獲取全路徑
	_conf.bSubDir = ((CButton*)GetDlgItem(IDC_CHECK_SUBDIR))->GetCheck() == TRUE;
	_conf.bFullPath = ((CButton*)GetDlgItem(IDC_CHECK_FULLPATH))->GetCheck() == TRUE;
	if (((CButton*)GetDlgItem(IDC_RADIO_SEMICOLON))->GetCheck() == TRUE)
	{
		_conf.nSepIndex = 1;
	}
	else
	{
		_conf.nSepIndex = 0;
	}

	char cSepStr = _conf.nSepIndex == 1 ? ';' : '\n';

	//獲取後綴
	CComboBox* pCombox = (CComboBox*)GetDlgItem(IDC_COMBO_SUFFIX);
	if (pCombox != nullptr)
	{
		CString strSuffix;
		pCombox->GetWindowText(strSuffix);
		_conf.vSuffixs.clear();
		_conf.vSuffixs.push_back(CMfcStrFile::CString2string(strSuffix));
	}
	_tstring sSuffix = VectorToString(_conf.vSuffixs);

	//測試時間
	clock_t start, finish;
	double duration;
	start = clock();

	//開始顯示進度
	//開始顯示進度
	CTaskBarProgress tbp(m_hWnd);
	CProgressInterface* ppi = &tbp;
	ppi->Start();
	/*********************************這裏增加主程序 開始***************************************/
	ppi->OutputInfo("Reading the files!");

	std::vector<_tstring> vFiles;
	std::string suffix = CStdStr::ws2s(sSuffix);
	getFiles(sDir, vFiles, suffix.c_str(), _conf.bSubDir);

	//將文件轉換
	for (auto it = vFiles.begin(); it != vFiles.end(); ++it)
	{
		if (!_conf.bFullPath)
		{
			*it = CStdStr::GetNameOfFile(*it);
		}

		*it += cSepStr;
	}

	CStdFile::SaveTXTFile(sFile, vFiles);
	/*********************************這裏增加主程序 結束***************************************/
	ppi->End();
	FlashWindow(TRUE);

	finish = clock();
	duration = double(finish - start) / CLOCKS_PER_SEC;
	int nMinutes = int(duration / 60);
	int nSeconds = (int)duration % 60;
	CString strTips;
	strTips.Format(_T("%d分%d秒%d毫秒"), nMinutes, nSeconds, (finish - start) % CLOCKS_PER_SEC);

	//保存log,當前時間以及耗時
	CString strTime, strDate; //獲取系統時間   
	CTime tm = CTime::GetCurrentTime();
	strTime = tm.Format("%Y年%m月%d日 %X");
	strDate = tm.Format("%Y年%m月%d日");

	_tstring sLogLine;
#ifdef NEED_FILE
	sLogLine = sFile;
#endif // NEED_FILE
#ifdef NEED_DIR
	sLogLine = sDir;
#endif // NEED_DIR
	//同時需要文件和文件夾時,記錄文件夾
	CString strSaveLine = _T("本次耗時:") + strTips + "\t" + CString(sDir.c_str()) + '\n';

	//保存日誌和配置文件
	_tstring strIniPath = GetIniPath();
	_tstring strExeDir = CStdStr::GetDirOfFile(strIniPath);
	CString strLogPath = CStdStr::AddSlashIfNeeded(strExeDir).c_str() + strDate + ".log";
	CStdioFile sStdFile(strLogPath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);
	sStdFile.SeekToEnd();
	setlocale(LC_CTYPE, "chs");//設定f
	sStdFile.WriteString(strSaveLine);
	setlocale(LC_ALL, "C"); //還原區域設定
	sStdFile.Close();
	WriteIniFile(strIniPath, _conf);
	AfxMessageBox(strTips);

	CDialogEx::OnOK();

更多的交流,歡迎留言。

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