C++ 設定檔案最近修改時間

來源:互聯網
上載者:User

標籤:local   get   dump   指定   檔案   檢查   nullptr   .cpp   bst   

利用VS開發C++項目,經常發現修改系統時間後,每次編譯過程會變得很慢,其原因就是當你把系統時間調到未來的一個時間點,然後有意或者無意編輯過一些代碼檔案,那麼這些檔案的時間戳記就停留在未來.

當你把系統時間調到現在後,編譯器每次編譯的時候,總會判定這些檔案是最新的,所以總是會把這些檔案編譯一遍,如果這些檔案中有某些被其他檔案引用,那麼會有更多的檔案被重新編譯,而且這種過程每次

編譯都會執行一遍,導致編譯速度降低.為瞭解決這個問題,我特意寫了一個小工具.

這個工具的原理跟簡單,找出指定目錄下時間戳記大於目前時間的檔案,並把它們的時間戳記設定為現在時間.

調用的時候,配合一個bat指令碼,把需要檢查的代碼目錄傳進去就好了,例如

echo offResetFileTime MsgDefine Serverpause

 

#include <io.h>#include <windows.h>#include <stdint.h>#include <vector>#include <string>#include <set>#include <stdio.h>#include <iostream>bool IsCompileFile(const std::string & extension){    static std::set<std::string> setComplieFileExtension = {".cpp", ".c", ".cc", ".h", ".hpp"};    return setComplieFileExtension.find(extension) != setComplieFileExtension.end();}std::string GetFileNameExtension(const std::string & fileName){    /*    DWORD dwAttrib = GetFileAttributes(fileName.c_str());    if (dwAttrib == INVALID_FILE_ATTRIBUTES)    {        return "";    }    if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)    {        return "";    }*/    size_t dotpos = fileName.find_last_of(".");    if(dotpos == std::string::npos)    {        return fileName;    }    return fileName.substr(dotpos, fileName.length() - dotpos);}bool CompareSystemTime(const SYSTEMTIME & lhs, const SYSTEMTIME & rhs){    if(lhs.wYear > rhs.wYear)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth > rhs.wMonth)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay > rhs.wDay)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay            && lhs.wHour > rhs.wHour)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay            && lhs.wHour == rhs.wHour && lhs.wMinute > rhs.wMinute)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay            && lhs.wHour == rhs.wHour && lhs.wMinute == rhs.wMinute && lhs.wSecond > rhs.wSecond)    {        return true;    }    else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay            && lhs.wHour == rhs.wHour && lhs.wMinute == rhs.wMinute && lhs.wSecond == rhs.wSecond && lhs.wMilliseconds > rhs.wMilliseconds)    {        return true;    }    return false;}void DumpSystemTime(const std::string & prefix, const SYSTEMTIME & t){    printf("%s %04d-%02d-%02d %02d:%02d:%02d\n", prefix.c_str(), t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);}void ResetFileTime(const std::string & dir){    WIN32_FIND_DATA fileInfo;    HANDLE hFile = nullptr;    char tmpPath[MAX_PATH] = { 0 };    sprintf_s(tmpPath, "%s\\*.*", dir.c_str());    if((hFile = FindFirstFile(tmpPath, &fileInfo)) == HANDLE(-1))    {        return;    }    do    {        if(fileInfo.dwFileAttributes & _A_SUBDIR)        {            if(strcmp(fileInfo.cFileName, ".") == 0 || strcmp(fileInfo.cFileName, "..") == 0)            {                continue;            }            sprintf_s(tmpPath, "%s\\%s", dir.c_str(), fileInfo.cFileName);            ResetFileTime(tmpPath);        }        else        {            sprintf_s(tmpPath, "%s\\%s", dir.c_str(), fileInfo.cFileName);            std::string extension = GetFileNameExtension(fileInfo.cFileName);            if(IsCompileFile(extension))            {                FILETIME lastWriteLocalFileTime;                FileTimeToLocalFileTime(&fileInfo.ftLastWriteTime, &lastWriteLocalFileTime);                SYSTEMTIME lastWriteLocalSysTime, nowTime;                FileTimeToSystemTime(&lastWriteLocalFileTime, &lastWriteLocalSysTime);                GetLocalTime(&nowTime);                if(CompareSystemTime(lastWriteLocalSysTime, nowTime))                {                    HANDLE file =  ::CreateFile(tmpPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);                    DumpSystemTime(fileInfo.cFileName, lastWriteLocalSysTime);                    FILETIME nowWriteLocalFileTime;                    SystemTimeToFileTime(&nowTime, &nowWriteLocalFileTime);                    FILETIME nowWriteSysFileTime;                    LocalFileTimeToFileTime(&nowWriteLocalFileTime, &nowWriteSysFileTime);                    BOOL ret = SetFileTime(file, &nowWriteSysFileTime, &nowWriteSysFileTime, &nowWriteSysFileTime);                    if(ret == TRUE)                    {                        printf("reset time succ.\n");                    }                    else                    {                        printf("reset time fail.error=%d\n", GetLastError());                    }                }            }        }    }    while(FindNextFile(hFile, &fileInfo) == TRUE);    FindClose(hFile);}int32_t main(int32_t argc, char *argv[]){    for(int32_t i = 1; i < argc; ++i)    {        std::string dir = argv[i];        ResetFileTime(dir);    }    return 0;}

 

C++ 設定檔案最近修改時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.