VC++根据路径遍历所有文件

最近做的一个项目,需要在VC下遍历指定路径的所有文件,和之前的Delphi所采用的方式一样:递归调用。直接贴代码

BOOL CLdScanFileDlg::DoScan( const CString& v_strDir )
{
	theApp.m_Errorlog.AddMsgLog(L"DoScan Start", false);

	wchar_t chSearchPath[MAX_LONG_PATH] = {0};
	wchar_t chSubDir[MAX_LONG_PATH] = {0};
	wchar_t chFilePath[MAX_LONG_PATH] = {0};
	wchar_t chTemp[MAX_LONG_PATH] = {0};
	wchar_t chSql[MAX_BUFF_LENGTH] = {0};
	struct _wfinddata_t c_file;
	long hFile;
	int iFileStatus = 0;
	static int iScanFileNum = 0;
	static int iEncFileNum = 0;
	static int iDecFileNum = 0;
	CString strInfo =L"";
	CStringList	DirList;
	CStringList FailedPathList;
	CLdSqlite* pDb = new CLdSqlite;

	// 读取当前时间
	SYSTEMTIME sysTime;
	CString strScanTime = L"";
	::GetLocalTime(&sysTime);
	strScanTime.Format(L"%4d%02d%02d%02d%02d%02d", sysTime.wYear, sysTime.wMonth,
		sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);

	// 更新目录扫描状态
	CString str = L"PathConfigDb";
	SetCurrentDataBase(str);
	pDb->Init(m_szDbPath, true);
	CString strDirTemp(v_strDir);
	strDirTemp.Replace(L"'", L"''");	// 替换单引号'为''
	// 更新配置库,扫描路径状态为1 代表正在扫描
	swprintf_s(chSql, L"update scan_status set scan_status = 1, scan_time = '%s' where scan_path='%s';", strScanTime, strDirTemp);
	pDb->ExcuteSql(chSql);
	pDb->Release();

	// 初始化当前扫描目录Db文件链接
	this->SetCurrentDataBase(v_strDir);
	pDb->Init(m_szDbPath, false);

	DirList.AddHead(v_strDir);
	while (!DirList.IsEmpty())
	{
		CString	strPath = DirList.RemoveHead();
		wcscpy(chSearchPath, strPath);
		wcscat(chSearchPath, L"\\*.*");

		hFile = _wfindfirst(chSearchPath, &c_file);
		if (-1 != hFile)
		{
			while (_wfindnext(hFile, &c_file) == 0)
			{
				WaitForSingleObject(g_hEvent, INFINITE);
				if (0 == wcscmp(L".", c_file.name) || 0 == wcscmp(L"..", c_file.name))
				{
					continue;
				}
				if (c_file.attrib & _A_SUBDIR)
				{
					wcscpy(chSubDir, strPath);
					wcscat(chSubDir, L"\\");
					wcscat(chSubDir, c_file.name);
					wcscpy(chTemp, chSubDir);
					// 过滤备份路径、回收站路径、绿盾安装目录路径
					if (wcscmp(_wcsupr(m_szBackUpPath), _wcsupr(chTemp)) == 0
						|| wcscmp(_wcsupr(m_szRecycle), _wcsupr(chTemp)) == 0
						|| wcscmp(_wcsupr(m_szLdTerm), _wcsupr(chTemp)) == 0)
					{
						continue;
					}
					DirList.AddHead(chSubDir);
				}
				else
				{
					//数据库写入
					wcscpy(chFilePath, strPath);
					wcscat(chFilePath, L"\\");
					wcscat(chFilePath, c_file.name);
					if (m_strReScanFile != L"")
					{	
						if (wcscmp(chFilePath, m_strReScanFile) != 0)
						{
							continue;
						}
						else
						{
							m_strReScanFile = L"";
						}						
					}
					// 扫描文件总数更新
					iScanFileNum++;
					strInfo.Format(L"%d", iScanFileNum);
					GetDlgItem(IDC_STATIC_SCAN)->SetWindowText(chFilePath);
					GetDlgItem(IDC_STATIC_SCAN_NUM)->SetWindowText(strInfo);
					//获取文件加解密状态
					/*m_strLog.Format(L"The Scan File is :%s", chFilePath);
					theApp.m_Errorlog.AddMsgLog(m_strLog, false);*/
					iFileStatus = this->FileDecryptStatus(chFilePath);
					if (iFileStatus != 1)
					{
						continue;
					}
					//替换特殊单引号
					CString strTemp(chFilePath);
					strTemp.Replace(L"'", L"''");

					//判断该数据是否存在
					swprintf_s(chSql, L"select count(*) from file_info where file_path='%s';", strTemp);
					if (pDb->DataExits(chSql) <= 0)
					{
						// 加密文件数更新
						iEncFileNum++;
						strInfo.Format(L"%d", iEncFileNum);
						GetDlgItem(IDC_STATIC_ENCNUM)->SetWindowText(strInfo);

						// 执行备份文件
						if (!DoBackUpFile(chFilePath))
						{
							ResetEvent(g_hEvent);
							theApp.m_Errorlog.AddMsgLog(L"Scan Stop", false);
							pDb->Release();
							delete pDb;
							pDb = NULL;
							GetDlgItem(IDC_BTN_STOP)->SetWindowText(L"继续");
							AfxMessageBox(L"剩余空间不足,备份失败,将暂停本次扫描");
							return FALSE;
						}

						// sql语句批量执行,默认累计满100条进行插入
						swprintf_s(chSql, L"insert into file_info values ('%s', %d, 0, 2, 0, '%s');", strTemp, iFileStatus, strScanTime);
						if (m_strFpList.GetSize() < m_iBatchInsertNum)
						{
							m_strFpList.AddHead(chSql);
						}
						else
						{
							pDb->ExcuteSqlList(m_strFpList);
							m_strFpList.RemoveAll();
							m_strFpList.AddHead(chSql);
						}
					}
					//Sleep(300);
				}
			}
			_findclose(hFile);
		}
	}
	//防止最后剩余文件未插入
	if (m_strFpList.GetSize() != 0)
	{
		theApp.m_Errorlog.AddMsgLog(L"Last Insert Start", false);
		pDb->ExcuteSqlList(m_strFpList);
		m_strFpList.RemoveAll();
		theApp.m_Errorlog.AddMsgLog(L"Last Insert Finish", false);
	}

	theApp.m_Errorlog.AddMsgLog(L"Scan Finish", false);
	//释放DB链接
	pDb->Release();
	delete pDb;
	pDb = NULL;
	return TRUE;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值