利用VS开发C++项目,经常发现修改系统时间后,每次编译过程会变得很慢,其原因就是当你把系统时间调到未来的一个时间点,然后有意或者无意编辑过一些代码文件,那么这些文件的时间戳就停留在未来.

当你把系统时间调到现在后,编译器每次编译的时候,总会判定这些文件是最新的(因为它的时间戳总是大于目标文件的时间戳),所以总是会把这些文件编译一遍,如果这些文件中有某些被其他文件引用,那么会有更多的文件被重新编译,而且这种过程每次编译都会执行一遍,导致编译速度降低.为了解决这个问题,我特意写了一个小工具.

这个工具的原理跟简单,找出指定目录下时间戳大于当前时间的文件,并把它们的时间戳设置为现在时间.

调用的时候,配合一个bat脚本,把需要检查的代码目录传进去就好了,例如

  1. echo off
  2. ResetFileTime MsgDefine Server
  3. pause
  1. #include <io.h>
  2. #include <windows.h>
  3. #include <stdint.h>
  4. #include <vector>
  5. #include <string>
  6. #include <set>
  7. #include <stdio.h>
  8. #include <iostream>
  9.  
  10. bool IsCompileFile(const std::string & extension)
  11. {
  12. static std::set<std::string> setComplieFileExtension = {".cpp", ".c", ".cc", ".h", ".hpp"};
  13. return setComplieFileExtension.find(extension) != setComplieFileExtension.end();
  14. }
  15.  
  16. std::string GetFileNameExtension(const std::string & fileName)
  17. {
  18. /*
  19. DWORD dwAttrib = GetFileAttributes(fileName.c_str());
  20. if (dwAttrib == INVALID_FILE_ATTRIBUTES)
  21. {
  22. return "";
  23. }
  24. if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
  25. {
  26. return "";
  27. }*/
  28. size_t dotpos = fileName.find_last_of(".");
  29. if(dotpos == std::string::npos)
  30. {
  31. return fileName;
  32. }
  33. return fileName.substr(dotpos, fileName.length() - dotpos);
  34. }
  35.  
  36. bool CompareSystemTime(const SYSTEMTIME & lhs, const SYSTEMTIME & rhs)
  37. {
  38. if(lhs.wYear > rhs.wYear)
  39. {
  40. return true;
  41. }
  42. else if(lhs.wYear == rhs.wYear && lhs.wMonth > rhs.wMonth)
  43. {
  44. return true;
  45. }
  46. else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay > rhs.wDay)
  47. {
  48. return true;
  49. }
  50. else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay
  51. && lhs.wHour > rhs.wHour)
  52. {
  53. return true;
  54. }
  55. else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay
  56. && lhs.wHour == rhs.wHour && lhs.wMinute > rhs.wMinute)
  57. {
  58. return true;
  59. }
  60. else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay
  61. && lhs.wHour == rhs.wHour && lhs.wMinute == rhs.wMinute && lhs.wSecond > rhs.wSecond)
  62. {
  63. return true;
  64. }
  65. else if(lhs.wYear == rhs.wYear && lhs.wMonth == rhs.wMonth && lhs.wDay == rhs.wDay
  66. && lhs.wHour == rhs.wHour && lhs.wMinute == rhs.wMinute && lhs.wSecond == rhs.wSecond && lhs.wMilliseconds > rhs.wMilliseconds)
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72.  
  73. void DumpSystemTime(const std::string & prefix, const SYSTEMTIME & t)
  74. {
  75. printf("%s %04d-%02d-%02d %02d:%02d:%02d\n", prefix.c_str(), t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
  76. }
  77.  
  78. void ResetFileTime(const std::string & dir)
  79. {
  80. WIN32_FIND_DATA fileInfo;
  81. HANDLE hFile = nullptr;
  82. char tmpPath[MAX_PATH] = { };
  83. sprintf_s(tmpPath, "%s\\*.*", dir.c_str());
  84. if((hFile = FindFirstFile(tmpPath, &fileInfo)) == HANDLE(-))
  85. {
  86. return;
  87. }
  88. do
  89. {
  90. if(fileInfo.dwFileAttributes & _A_SUBDIR)
  91. {
  92. if(strcmp(fileInfo.cFileName, ".") == || strcmp(fileInfo.cFileName, "..") == )
  93. {
  94. continue;
  95. }
  96. sprintf_s(tmpPath, "%s\\%s", dir.c_str(), fileInfo.cFileName);
  97. ResetFileTime(tmpPath);
  98. }
  99. else
  100. {
  101. sprintf_s(tmpPath, "%s\\%s", dir.c_str(), fileInfo.cFileName);
  102. std::string extension = GetFileNameExtension(fileInfo.cFileName);
  103. if(IsCompileFile(extension))
  104. {
  105. FILETIME lastWriteLocalFileTime;
  106. FileTimeToLocalFileTime(&fileInfo.ftLastWriteTime, &lastWriteLocalFileTime);
  107. SYSTEMTIME lastWriteLocalSysTime, nowTime;
  108. FileTimeToSystemTime(&lastWriteLocalFileTime, &lastWriteLocalSysTime);
  109. GetLocalTime(&nowTime);
  110. if(CompareSystemTime(lastWriteLocalSysTime, nowTime))
  111. {
  112. HANDLE file = ::CreateFile(tmpPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
  113.  
  114. DumpSystemTime(fileInfo.cFileName, lastWriteLocalSysTime);
  115. FILETIME nowWriteLocalFileTime;
  116. SystemTimeToFileTime(&nowTime, &nowWriteLocalFileTime);
  117. FILETIME nowWriteSysFileTime;
  118. LocalFileTimeToFileTime(&nowWriteLocalFileTime, &nowWriteSysFileTime);
  119. BOOL ret = SetFileTime(file, &nowWriteSysFileTime, &nowWriteSysFileTime, &nowWriteSysFileTime);
  120. if(ret == TRUE)
  121. {
  122. printf("reset time succ.\n");
  123. }
  124. else
  125. {
  126. printf("reset time fail.error=%d\n", GetLastError());
  127. }
  128. }
  129. }
  130. }
  131. }
  132. while(FindNextFile(hFile, &fileInfo) == TRUE);
  133. FindClose(hFile);
  134. }
  135.  
  136. int32_t main(int32_t argc, char *argv[])
  137. {
  138. for(int32_t i = ; i < argc; ++i)
  139. {
  140. std::string dir = argv[i];
  141. ResetFileTime(dir);
  142. }
  143. return ;
  144. }

C++ 设置文件最近修改时间的更多相关文章

  1. touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令

    1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...

  2. hdfs文件按修改时间下载

    应用于:对于不同用户创建的表目录,进行文件的下载,程序中执行hadoop cat命令 下载文件到本地,随后通过ftp传至目标服务器,并将hdfs文件目录的修改时间存入mysql中.每次修改前将mysq ...

  3. png文件格式详解,获取文件的修改时间,创作时间

    http://dev.gameres.com/Program/Visual/Other/PNGFormat.htmhttp://www.360doc.com/content/11/0428/12/10 ...

  4. python 获取文件的修改时间

    os.path.getmtime(name) #获取文件的修改时间 os.stat(path).st_mtime#获取文件的修改时间 os.stat(path).st_ctime #获取文件修改时间 ...

  5. Powershell按文件最后修改时间删除多余文件

    Powershell按文件最后修改时间删除多余文件 1. 删除目录内多余文件,目录文件个数大于$count后,按最后修改时间倒序排列,删除最旧的文件. Sort-Object -Property La ...

  6. C# 读取文件的修改时间、访问时间、创建时间

    C# 获取文件的各个时间如下: 表2<ccid_nobr> 属性 功能和用途 Attributes 返回和文件相关的属性值,运用了FileAttributes枚举类型值 CreationT ...

  7. C#实现对指定文件夹中文件按修改时间排序

    string path = "~/Document/Introduction/团队管理制度/";            DirectoryInfo dirinfo = new Di ...

  8. TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间

    使用函数: System.IOUtils.TDirectory.GetCreationTime//获取创建时间 System.IOUtils.TDirectory.SetCreationTime//设 ...

  9. C#获取ftp文件最后修改时间

    public static DateTime GetFileModifyDateTime(string ftpServerIP,string ftpFolder,string ftpUserID,st ...

随机推荐

  1. 洛谷noip 模拟赛 day1 T3

    T7983 大芳的逆行板载 题目背景 大芳有一个不太好的习惯:在车里养青蛙.青蛙在一个n厘米(11n毫米s)的Van♂杆子上跳来跳去.她时常盯着青蛙看,以至于突然逆行不得不开始躲交叉弹.有一天他突发奇 ...

  2. input 输入框提示信息

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 开发框架 springBoot

    1.多个环境的配置文件 在application.yml 中配置需要调用的配置文件 spring: profiles: active: dev 运行方式的,先运行application.yml 再根据 ...

  4. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  5. #424 Div2 C

    #424 Div2 C 题意 给出 k 个分数,初始分数未知,按顺序把这 k 个分数加到初始分数上,已知 n 个加入分数后的结果(无序),问初始分数有多少种可能. 分析 也就是说这 n 个结果,它们之 ...

  6. 宠物收养所 (SBT)

    宠物收养所 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得 ...

  7. python 传不可变对象 or 可变对象

    可更改(mutable)与不可更改(immutable)对象 在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象 ...

  8. DB2如何调整表空间大小

    DB2如何调整表空间大小 刚刚接到客户那边打的电话,程序一直报错,所有的业务都做不了,拷贝了一份应用服务器(weblogic)的日志,日志里显示: WARN : 2009-06-18 16:24:32 ...

  9. 集合框架(04)HashMap

    集合Map的简单方法:该集合存储键值对,一对一对往里面存,而且要保证健的唯一性 1.添加 put(K key,V value) putAll(Map<? Extends k, ? extends ...

  10. Bluetooth篇 开发实例之七 匹配&UUID

    匹配和通信是两回事. 1.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出.但是可以通过反射来获取. 知道这两个API的 ...