吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1273|回复: 3
收起左侧

[C&C++ 原创] 修改文件创建修改日期的一个小工具代码

[复制链接]
soso101 发表于 2023-11-14 08:21

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

#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[1]);

    int wYear = atoi(argv[2]);
    int wMonth = atoi(argv[3]);
    int wDay = atoi(argv[4]);
    int wHour = atoi(argv[5]);
    int wMinute = atoi(argv[6]);
    int wSecond = atoi(argv[7]);

    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;
    }
}

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
o9981k 发表于 2023-11-14 12:04
提示: 作者被禁止或删除 内容自动屏蔽
cyh1119 发表于 2023-11-17 15:51
lijiaa1221 发表于 2023-11-24 12:53
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 17:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表