soso101 发表于 2023-11-14 08:21

修改文件创建修改日期的一个小工具代码

修改文件创建日期、最后修改日期的一个小工具代码,新人报到,欢迎各位大佬指导

```cpp
#include <locale.h>
#include <locale>
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <io.h>
#include <string>

using namespace std;

void getAllFiles(std::string path, std::vector<std::string>& filePaths, std::vector<std::string>& directoryPaths);
bool changeFileTime(const char* filename, bool isFile, int wYear, int wMonth, int wDay, int wHour, int wMinute, int wSecond);

int main(int argc, char** argv) {

    std::string usage("Usage: changeft Path Year Moth Day Hour Minute Second");
    if (argc != 8)
    {
      std::cout << usage << std::endl;
      return 0;
    }
    std::string path(argv);
   
    int wYear = atoi(argv);
    int wMonth = atoi(argv);
    int wDay = atoi(argv);
    int wHour = atoi(argv);
    int wMinute = atoi(argv);
    int wSecond = atoi(argv);

    std::cout << "Start ..." << endl;
    std::vector<std::string> filePaths;
    std::vector<std::string> directoryPaths;
    getAllFiles(path, filePaths, directoryPaths);
    for (std::vector<std::string>::const_iterator iter = filePaths.cbegin(); iter != filePaths.cend(); iter++) {
      std::string path(*iter);
      const char* path_c = path.c_str();
      bool flag = changeFileTime(path_c, true, wYear, wMonth, wDay, wHour, wMinute, wSecond);
      cout << "File Changed -> " + std::to_string(flag) + "," + path << endl;
    }
    for (std::vector<std::string>::const_iterator iter = directoryPaths.cbegin(); iter != directoryPaths.cend(); iter++) {
      std::string path(*iter);
      const char* path_c = path.c_str();
      bool flag = changeFileTime(path_c, false, wYear, wMonth, wDay, wHour, wMinute, wSecond);
      cout << "Directory Changed -> " + std::to_string(flag) + "," + path << endl;
    }

    std::cout << "Finish !" << endl;
    return 0;
}
void getAllFiles(std::string path, std::vector<std::string>& filePaths, std::vector<std::string>& directoryPaths) {
    intptr_t fHandle = 0;
    struct _finddata_t fInfo;
    std::string s;
    if ((fHandle = _findfirst(s.assign(path).append("\\*").c_str(), &fInfo)) != -1) {
      do {
            const char* fName = fInfo.name;
            if ((fInfo.attrib & _A_SUBDIR)) {
                if (strcmp(fName, ".") != 0 && strcmp(fName, "..") != 0) {
                  directoryPaths.push_back(s.assign(path).append("/").append(fName));
                  getAllFiles(s.assign(path).append("/").append(fName), filePaths, directoryPaths);
                }
            }
            else {
                filePaths.push_back(s.assign(path).append("/").append(fName));
            }
      } while (_findnext(fHandle, &fInfo) == 0);//find next, if success return 0 or fail return -1
      _findclose(fHandle);
    }
}

bool changeFileTime(const char* filename,
    bool isFile, //file:true,directory:false
    int wYear,
    int wMonth,
    int wDay,
    int wHour,
    int wMinute,
    int wSecond) {
    SYSTEMTIME createTime;
    GetSystemTime(&createTime);
    createTime.wYear = wYear;      //changes the year
    createTime.wMonth = wMonth;   //changes the month
    createTime.wDay = wDay;       //changes the day
    createTime.wHour = wHour;      //changes the hour
    createTime.wMinute = wMinute;    //changes the minute
    createTime.wSecond = wSecond;    //changes the second

    SYSTEMTIME lastWriteTime;
    GetSystemTime(&lastWriteTime);
    lastWriteTime.wYear = wYear;   //changes the year
    lastWriteTime.wMonth = wMonth;//changes the month
    lastWriteTime.wDay = wDay;    //changes the day
    lastWriteTime.wHour = wHour;   //changes the hour
    lastWriteTime.wMinute = wMinute; //changes the minute
    lastWriteTime.wSecond = wSecond; //changes the second

    SYSTEMTIME lastAccessTime;
    GetSystemTime(&lastAccessTime);
    lastAccessTime.wYear = wYear;   //changes the year
    lastAccessTime.wMonth = wMonth;//changes the month
    lastAccessTime.wDay = wDay;    //changes the day
    lastAccessTime.wHour = wHour;   //changes the hour
    lastAccessTime.wMinute = wMinute; //changes the minute
    lastAccessTime.wSecond = wSecond; //changes the second

    //convert time to filetime
    FILETIME lastWriteFiletime;
    SystemTimeToFileTime(&lastWriteTime, &lastWriteFiletime);

    FILETIME createFiletime;
    SystemTimeToFileTime(&createTime, &createFiletime);

    FILETIME lastAccessFileTime;
    SystemTimeToFileTime(&lastAccessTime, &lastAccessFileTime);

    if (isFile) {
      //get the file handle
      HANDLE hFile = CreateFile(filename,
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
            NULL, OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL, NULL);

      //set time to the file
      bool flag = SetFileTime(hFile, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);

      //close handle.
      CloseHandle(hFile);

      if (!flag) {
            std::printf("fail, error code: %d -> ", GetLastError());
      }
      return flag;
    }
    else {
      //get the directory handle
      HANDLE hDir = CreateFile(filename,
            GENERIC_READ | GENERIC_WRITE,
            0 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
            NULL, OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS, NULL);

      //set time to the directory
      bool flag = SetFileTime(hDir, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);

      //close handle.
      CloseHandle(hDir);

      if (!flag) {
            std::printf("fail, error code: %d -> ", GetLastError());
      }
      return flag;
    }
}
```

o9981k 发表于 2023-11-14 12:04

cyh1119 发表于 2023-11-17 15:51

试试就试试{:1_918:}

lijiaa1221 发表于 2023-11-24 12:53

可以可以,有时候有用
页: [1]
查看完整版本: 修改文件创建修改日期的一个小工具代码