最近遇到这个需求,在文件名前增加或者删除日期。
如下图所示,功能非常简单,特此记录主要代码。
主要代码如下:
#include <Windows.h>
//----------- 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, CStdStr::s2ws(szBuf).c_str(), _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"),
stLocal1.wYear, stLocal1.wMonth, stLocal1.wDay,
stLocal2.wHour, stLocal2.wMinute);
wsprintf(lpszLastWriteTime, _T("%d%02d%02d-%02d%02d"),
stLocal1.wYear, stLocal1.wMonth, stLocal1.wDay,
stLocal3.wHour, stLocal3.wMinute);
return TRUE;
}
_tstring GetModifyTime(_tstring strFilePath)
{
HANDLE hFile;
TCHAR szCreationTime[30], szLastAccessTime[30], szLastWriteTime[30];
hFile = CreateFile(strFilePath.c_str(), 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;
}
//printf("%s\n%s\n%s\n", szCreationTime, szLastAccessTime, szLastWriteTime);
CloseHandle(hFile);
return szLastWriteTime;
}
_tstring AddPreFix(_tstring strFileName)
{
_tstring cTime(GetModifyTime(strFileName));
_tstring strDir = CStdStr::GetDirOfDir(strFileName);
_tstring strNewName = cTime + CStdStr::GetNameOfFile(strFileName);
return strDir + _T("\\") + strNewName;
}
_tstring JustProcessFileName(_tstring strFileName)
{
_tstring strOldFile(strFileName);
_tstring strNewName;
/************************************************************************/
_tstring strNewFile = AddPreFix(strFileName);
_tstring cTime(GetModifyTime(strFileName));
if (CStdStr::GetNameOfFile(strOldFile).substr(0, cTime.length()) != CStdStr::GetNameOfFile(strNewFile).substr(0, cTime.length()))
{
return strNewFile;
}
else
{
_tstring strR(strOldFile);
strR = CStdStr::GetNameOfFile(strOldFile, false);
strR = strR.substr(cTime.length());
strR = CStdStr::AddSlashIfNeeded(CStdStr::GetDirOfFile(strOldFile)) + strR + CStdStr::GetSuffixOfFile(strOldFile);
return strR;
}
/************************************************************************/
}
size_t ProcessVectorFiles(std::vector<_tstring>& vRrlxFiles)
{
const size_t argc = vRrlxFiles.size();
for (int i = 0; i < argc; ++i)
{
_tstring strFile = vRrlxFiles[i];
_tstring suffix = CStdStr::GetSuffixOfFile(strFile);
strFile = CStdStr::ToUpperLower(strFile);
if (CStdFile::IfAccessFile(strFile))
{
_tstring strOldFile(strFile);
_tstring strNewFile(JustProcessFileName(vRrlxFiles[i].c_str()));
rename(CStdStr::ws2s(strOldFile).c_str(), CStdStr::ws2s(strNewFile).c_str());
vRrlxFiles[i] = strNewFile;
}
}
return 0;
}
更多的交流,欢迎留言。