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

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

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

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

echo off
ResetFileTime MsgDefine Server
pause
#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] = { };
sprintf_s(tmpPath, "%s\\*.*", dir.c_str());
if((hFile = FindFirstFile(tmpPath, &fileInfo)) == HANDLE(-))
{
return;
}
do
{
if(fileInfo.dwFileAttributes & _A_SUBDIR)
{
if(strcmp(fileInfo.cFileName, ".") == || strcmp(fileInfo.cFileName, "..") == )
{
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 = ; i < argc; ++i)
{
std::string dir = argv[i];
ResetFileTime(dir);
}
return ;
}

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. ajax数据读取和绑定

    如何进行ajax数据读取和绑定呢? 首先创建一个AJAX对象 实现数据绑定 实现隔行变色 编写表格排序的方法(实现按照年龄这一列进行排序) 通过文档碎片,把排序后的最新顺序,重新添加到tBody中(通 ...

  2. 生成 RSA 私钥及公钥

    $ openssl # 进入 OpenSSL 程序 OpenSSL> genrsa -out rsa_private_key.pem 1024 # 生成私钥 OpenSSL> pkcs8 ...

  3. 汕头市队赛SRM14 T3覆盖

    我们可以考虑两种情况 区间之间不相重叠 和 重叠 f[i][j]表示以当前最后一个区间以 i 结尾 并且选了 j 个区间 不相重叠的话 只要选 1-i-w 的max再加上 包含i在内的前四个数的和 相 ...

  4. [ CodeVS冲杯之路 ] P1501

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/1501/ 水题一道 直接dfs,记录上当前深度,到了叶子节点就更新答案,并且每个节点将当前深度的计数+1,答案即为ma ...

  5. 搭建github服务器

    https://about.gitlab.com/downloads/#centos6

  6. (二十)linux中i2c的ioctl,write,read函数的使用

    一.ioctl函数的使用:原型:struct ioctl(struct file *file,unsigned int cmd,unsigned long arg);cmd有I2C_SLAVE,I2C ...

  7. EXCEL匹配结果match并跳转链接hyperlink

    1,有时候想要搜索另一个表格中含有相同内容的项,然后跳转到搜索结果单元. 需要用到两个函数,MATCH和HYPERLINK 2,A表格如下 B表格如下 3,在B2单元格中输入函数 =HYPERLINK ...

  8. react 使用Form组件如何清空上一次操作

    最近在做一个表单联查时候,总是会发现后一个选择器会记住上一次选择的值 ,这会导致前一级选择器已经做出更新后,后一级选择器却还记住上一次的操作, 这里有个方法可以在上级选择器事件操作时清空Form组件的 ...

  9. 51nod 1007 正整数分组【01背包变形】

    1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...

  10. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...