根据文件修改时间分类文件

85 篇文章 4 订阅

这是早年写的代码,谨此记录吧。

//----------- Error Handling Function -------------------
void error(LPSTR lpszFunction)
{
	CHAR szBuf[80];
	DWORD dw = GetLastError();

	sprintf(szBuf, "%s failed: GetLastError returned %u\n",
		lpszFunction, dw);

	MessageBox(NULL, CString(szBuf), _T("Error"), MB_OK);
	ExitProcess(dw);
}
//--------------------------------------------------------

BOOL GetFileTime(HANDLE hFile, LPWSTR lpszCreationTime, LPWSTR lpszLastAccessTime, LPWSTR lpszLastWriteTime)
{
	FILETIME ftCreate, ftAccess, ftWrite;
	SYSTEMTIME stUTC1, stLocal1, stUTC2, stLocal2, stUTC3, stLocal3;

	// -------->获取 FileTime
	if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
	{
		error("GetFileTime()");
		return FALSE;
	}
	//---------> 转换: FileTime --> LocalTime
	FileTimeToSystemTime(&ftCreate, &stUTC1);
	FileTimeToSystemTime(&ftAccess, &stUTC2);
	FileTimeToSystemTime(&ftWrite, &stUTC3);

	SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1);
	SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2);
	SystemTimeToTzSpecificLocalTime(NULL, &stUTC3, &stLocal3);

	// ---------> Show the  date and time.
	wsprintf(lpszCreationTime, _T("%d%02d%02d-%02d%02d"),
		stLocal1.wYear, stLocal1.wMonth, stLocal1.wDay,
		stLocal1.wHour, stLocal1.wMinute);
	wsprintf(lpszLastAccessTime, _T("%d%02d%02d-%02d%02d"),
		stLocal2.wYear, stLocal2.wMonth, stLocal2.wDay,
		stLocal2.wHour, stLocal2.wMinute);
	wsprintf(lpszLastWriteTime, _T("%d%02d%"),stLocal3.wYear, stLocal3.wMonth);
	return TRUE;
}

CString GetModifyTime(LPCWSTR strFilePath)
{
	HANDLE hFile;
	TCHAR szCreationTime[30], szLastAccessTime[30], szLastWriteTime[30];
	hFile = CreateFile(strFilePath, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	GetFileTime(hFile, szCreationTime, szLastAccessTime, szLastWriteTime);
	if (hFile == INVALID_HANDLE_VALUE){
		error("GetLastWriteTime()");
		return 0;
	}
	CloseHandle(hFile);

	return szLastWriteTime;
}
/******************************公共变量**********************************/
CString g_strDir;
ITaskbarList3* m_pTaskbarList;
CMutex g_Mutex;
std::map<std::string, std::string> g_mLogs;
/************************************************************************/


int CClassifyFilesDlg::MoveFilesByTime(const char* pStr, const char* pSavePath, const int& nProcessIndex)
{
	std::string strDir = CMfcStrFile::CString2Char(g_strDir);

	std::string strRootDir = CStdStr::AddSlashIfNeeded(strDir);

	if (_access(strRootDir.c_str(), 0) != 0)
	{
		if (!CreateDirectory(CString(strRootDir.c_str()), NULL))
		{
			return -1;
		}
	}

	CString strLastWriteTime = GetModifyTime(CString(pStr));
	std::string strYearMonth = std::string(CMfcStrFile::CString2Char(strLastWriteTime));

	std::string strSaveDir = strRootDir + strYearMonth;

	if (_access(strSaveDir.c_str(), 0) != 0)
	{
		if (!CreateDirectory(CString(strSaveDir.c_str()), NULL))
		{
			return -1;
		}
	}

	std::string strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(pStr);

	int nNum = 0;
	if (CStdFile::IfAccessFile(strSavePath))
	{
		do
		{
			++nNum;
			strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(pStr, false) + CStdTpl::ConvertToString(nNum) + CStdStr::GetSuffixOfFile(pStr);

		} while (CStdFile::IfAccessFile(strSavePath));
	}


	if (rename(pStr, strSavePath.c_str()) < 0)
	{

	}

	g_Mutex.Lock();
	g_mLogs.insert(std::make_pair(std::string(pStr), strSavePath));
	g_Mutex.Unlock();

	return 0;
}

int CClassifyFilesDlg::MovePicsByTime(const char* pStr, const char* pSavePath, const int& nProcessIndex)
{
	std::string strRootDir = CStdStr::AddSlashIfNeeded(CMfcStrFile::CString2Char(g_strDir));

	if (_access(strRootDir.c_str(), 0) != 0)
	{
		if (!CreateDirectory(CString(strRootDir.c_str()), NULL))
		{
			return -1;
		}
	}

	std::string strPicTime;
	std::string strYearMonth;

	if (Get_ExifTime(pStr, strPicTime))
	{
		strYearMonth = strPicTime.substr(0, 7);
		strYearMonth = CStdStr::TrimAll(strYearMonth, ':');
	}
	else
	{
		CString strLastWriteTime = GetModifyTime(CString(pStr));
		strYearMonth = std::string(CMfcStrFile::CString2Char(strLastWriteTime));
	}

	std::string strSaveDir = strRootDir + strYearMonth;

	if (_access(strSaveDir.c_str(), 0) != 0)
	{
		if (!CreateDirectory(CString(strSaveDir.c_str()), NULL))
		{
			return -1;
		}
	}

	std::string strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(pStr);

	if (CStdFile::IfAccessFile(strSavePath))
	{
		int nNum = 0;
		do
		{
			++nNum;
			strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(pStr, false) + CStdTpl::ConvertToString(nNum) + CStdStr::GetSuffixOfFile(pStr);

		} while (CStdFile::IfAccessFile(strSavePath));
	}

	if (strSavePath != std::string(pStr))
	{
		rename(pStr, strSavePath.c_str());
	}

	g_Mutex.Lock();
	g_mLogs.insert(std::make_pair(std::string(pStr), strSavePath));
	g_Mutex.Unlock();

	return 0;
}

int CClassifyFilesDlg::RestoreFile(const char* pStr, const char* pSavePath, const int& nProcessIndex)
{
	std::string strOldPath = g_mLogs[std::string(pStr)];
	std::string strOldDir = CStdStr::GetDirOfFile(strOldPath);
	if (_access(strOldDir.c_str(), 0) != 0)
	{
		CStdDir::CreateDir(strOldDir);
	}

	if (strOldPath != std::string(pStr))
	{
		rename(pStr, strOldPath.c_str());
	}

	return 0;
}

更多的交流,欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值