批量修改文件时间

其实这个需求都要从某一届的毕业答辩开始说起,当时有个同学打开了目录后,下面有个老师说你这个最后修改日期怎么在暑假,你暑假就做完了吗(我们的选题都是9月多,尽管后面题目可以商量)

如果你要修改目录本身,那么你应该关闭目录,然后再使用脚本

 cmd

如果失败了,就修改一下文件的编码

utf-8->ANSI

@ echo off
::target modify folder
set modify="新建文件夹"

::%date:~5,2%/%date:~8,2%/%date:~0,4% represents today
::yyyy represents year
::mm represents month
::dd represents day
::hh24 represents hour(24-hour clock)
::MM represents minute
::ss represents second

::yyyy/mm/dd represents yyyy/mm/dd 00:00:00
::hh24:MM:ss represents %date:~5,2%/%date:~8,2%/%date:~0,4% hh24:MM:ss

::change the follow parameter to the format of yyyy/mm/dd hh24:MM:ss or yyyy/mm/dd or hh24:MM:ss
::for example "2019/11/13 22:00:00"
:: "2019/11/13" represents 2019/11/13 00:00:00
:: "22:00:00" represents today 22:00:00

::last_write_time,default is today and current time
set LastWriteTime="%date:~5,2%/%date:~8,2%/%date:~0,4% %time:~0,2%:%time:~3,2%:%time:~6,2%"
::CreationTime,default is today and current time
set CreationTime="%date:~0,4%/%date:~5,2%/%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%"
::LastAccessTime,default is today and current time
set LastAccessTime="%date:~0,4%/%date:~5,2%/%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%"


powershell.exe -WindowStyle Hidden -command "Get-childitem -Path '%modify%' –Force -Recurse |  ForEach-Object { $_.LastWriteTime = '%LastWriteTime%'; $_.CreationTime = '%CreationTime%'; $_.LastAccessTime= '%LastAccessTime%' };IF(($modify_dir=Get-Item '%modify%') -is [IO.DirectoryInfo]){$modify_dir.LastWriteTime = '%LastWriteTime%'; $modify_dir.CreationTime = '%CreationTime%'; $modify_dir.LastAccessTime= '%LastAccessTime%'}"

 powershell

如果失败了就用管理员权限打开powershell,执行

Set-ExecutionPolicy RemoteSigned,然后按y

# target modify folder
$modify="C:\Users\Nightmare\Desktop\新建文件夹 - 副本"

<#
Get-Date represents today

yyyy represents year
mm represents month
dd represents day
hh24 represents hour(24-hour clock)
MM represents minute
ss represents second

yyyy/mm/dd represents yyyy/mm/dd 00:00:00
hh24:MM:ss represents %date:~5,2%/%date:~8,2%/%date:~0,4% hh24:MM:ss

change the follow parameter to the format of yyyy/mm/dd hh24:MM:ss or yyyy/mm/dd or hh24:MM:ss
for example "2019/11/13 22:00:00" represents now
"2019/11/13" represents 2019/11/13 00:00:00
"22:00:00" represents today 22:00:00
#>

# last_write_time,default is today and current time
# for example
# set LastWriteTime="2019/11/13 22:00:00"
$LastWriteTime=Get-Date
# CreationTime,default is today and current time
$CreationTime=Get-Date
# LastAccessTime,default is today and current time
$LastAccessTime=Get-Date

Get-childitem -Path $modify -Recurse | ForEach-Object { $_.LastWriteTime = $LastWriteTime; $_.CreationTime = $CreationTime; $_.LastAccessTime= $LastAccessTime }
IF(($modify_dir=Get-Item $modify) -is [IO.DirectoryInfo]){
  $modify_dir.LastWriteTime=$LastWriteTime;
  $modify_dir.CreationTime = $CreationTime; 
  $modify_dir.LastAccessTime= $LastAccessTime
}

C++ 

#include <iostream>
#include <string>
#include <queue>
#include <ATLComTime.h>
#include <filesystem>
using namespace std;
using namespace std::filesystem;

class FileTimeModify {
private:
	/**
	 * 修改文件/目录的时间
	 * @param modify_path 路径
	 * @param ctime 创建时间
	 * @param atime 最后访问时间
	 * @param mtime 最后修改时间
	 * @return 修改是否成功
	 */
	static bool modify(
		const string& modify_path,
		const FILETIME& ctime,
		const FILETIME& atime,
		const FILETIME& mtime) {
		HANDLE handle = CreateFile(
			modify_path.c_str(),
			GENERIC_READ | GENERIC_WRITE,
			FILE_SHARE_READ | FILE_SHARE_DELETE,
			NULL,
			OPEN_EXISTING,
			FILE_FLAG_BACKUP_SEMANTICS,
			NULL);
		try {
			if (INVALID_HANDLE_VALUE == handle) {
				CloseHandle(handle);
				return false;
			}
			BOOL retval = SetFileTime(handle, &ctime, &atime, &mtime);
			CloseHandle(handle);
			return retval;
		}
		catch (...) {
			CloseHandle(handle);
			return false;
		}
		CloseHandle(handle);
		return true;
	}
	
public:
	/**
	 * 判断闰年
	 * @param year 闰年
	 * @return 是不是闰年
	 */
	static bool is_leap(const int& year) {
		if (year % 172800 == 0) {
			return true;
		}
		if (year % 3200 == 0) {
			return false;
		}
		return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
	}
	/**
	 * 字符串转文件时间
	 * @param s 字符串yyyy/mm/dd hh24:MM:ss格式
	 * @param result 文件时间
	 * @return 是不是转化成功
	 */
	static bool string2file_time(const string& s, FILETIME& result) {
		SYSTEMTIME temp;
		if (sscanf(s.c_str(), "%hu/%hu/%hu %hu:%hu:%hu",
			&temp.wYear,
			&temp.wMonth,
			&temp.wDay,
			&temp.wHour,
			&temp.wMinute,
			&temp.wSecond) != 6) {
			return false;
		}
		if (0 <= temp.wHour && temp.wHour <= 60 &&
			0 <= temp.wMinute && temp.wMinute <= 60 &&
			0 <= temp.wSecond && temp.wSecond <= 60 &&
			1 <= temp.wMonth && temp.wMonth <= 12 &&
			1 <= temp.wDay && temp.wDay <= 31) {
			if (is_leap(temp.wYear)) {
				if (temp.wDay > 29) {
					return false;
				}
			}
			else {
				if (temp.wDay > 28) {
					return false;
				}
			}
		}
		else {
			return false;
		}
		temp.wMilliseconds = 0;
		SystemTimeToFileTime(&temp, &result);
		return true;
	}
	static bool file_time2string(const FILETIME& s, string& result) {
		char str[50];
		SYSTEMTIME rtime;
		FILETIME ltime;

		memset(str, 0, 50);
		FileTimeToLocalFileTime(&s, &ltime);
		FileTimeToSystemTime(&ltime, &rtime);        //将文件时间转化为系统时间
		if (sprintf(str, "%04u/%02u/%02u %02u:%02u:%02u", rtime.wYear, rtime.wMonth, rtime.wDay, rtime.wHour, rtime.wMinute, rtime.wSecond)<0) {
			return false;
		}
		result.assign(str);
		return true;
	}
	/**
	 * 修改文件/目录的时间
	 * @param modify_path 路径
	 * @param creation_time 创建时间
	 * @param last_access_time 最后访问时间
	 * @param last_write_time 最后修改时间
	 * @return 修改是否成功
	 */
	static bool modify(
		const string& modify_path,
		const string& creation_time,
		const string& last_access_time,
		const string& last_write_time) {
		FILETIME ctime, atime, mtime;
		if (!string2file_time(creation_time, ctime) ||
			!string2file_time(last_access_time, atime) ||
			!string2file_time(last_write_time, mtime)) {
			return false;
		}
		return modify(modify_path, ctime, atime, mtime);
	}
	/**
	 * 递归修改文件/目录的时间
	 * @param search_path 路径
	 * @param creation_time 创建时间
	 * @param last_access_time 最后访问时间
	 * @param last_write_time 最后修改时间
	 * @return 修改是否成功
	 */
	static bool modify_path(
		const string& search_path,
		const string& creation_time,
		const string& last_access_time,
		const string& last_write_time) {

		FILETIME ctime, atime, mtime;
		if (!string2file_time(creation_time, ctime) ||
			!string2file_time(last_access_time, atime) ||
			!string2file_time(last_write_time, mtime)) {
			return false;
		}
		if (!exists(search_path)) {
			cout << "文件/目录 不存在" << endl;
			return false;
		}
		bool flag;
		try {
			queue<path> q;
			q.push(search_path);
			while (!q.empty()) {
				path current_path = q.front();
				q.pop();
				//修改第一次入队的文件/目录和后续的目录
				flag = modify(current_path.string(), ctime, atime, mtime);
				if (!flag) {
					cout << "modify " << current_path.string() << " error" << endl;
				}
				//是目录
				if (is_directory(current_path)) {
					//遍历目录
					for (auto& it : directory_iterator(current_path)) {
						if (is_directory(it.path())) {
							//目录加入队列
							q.push(it.path());
						}
						else {
							//修改文件
							flag = modify(it.path().string(), ctime, atime, mtime);
							if (!flag) {
								cout << "modify " << it.path().string() << " error" << endl;
							}
						}
					}
				}
			}
		}
		catch (...) {
			cout << "发生了未知错误" << endl;
			return false;
		}
		return true;
	}
	/**
	 * 获得文件时间
	 * @param search_path 路径
	 * @param creation_time 创建时间
	 * @param last_access_time 最后访问时间
	 * @param last_write_time 最后修改时间
	 * @return 是否成功
	 */
	static bool get_file_time(
		const string& search_path,
		string& creation_time,
		string& last_access_time,
		string& last_write_time) {
		FILETIME ctime, atime, mtime;
		HANDLE handle = CreateFile(
			search_path.c_str(),
			GENERIC_READ,
			FILE_SHARE_READ | FILE_SHARE_DELETE,
			NULL,
			OPEN_EXISTING,
			FILE_FLAG_BACKUP_SEMANTICS,
			NULL);
		try {
			if (INVALID_HANDLE_VALUE == handle) {
				CloseHandle(handle);
				return false;
			}
			if (!GetFileTime(handle, &ctime, &atime, &mtime)) {
				CloseHandle(handle);
				return false;
			}
			if (!file_time2string(ctime, creation_time) ||
				!file_time2string(atime, last_access_time) ||
				!file_time2string(mtime, last_write_time)) {
				CloseHandle(handle);
				return false;
			}
			CloseHandle(handle);
			return true;
		}
		catch (...) {
			CloseHandle(handle);
			return false;
		}
		CloseHandle(handle);
		return true;
	}
};
int main() {
	//bfs("E:\\sort");
	string path = "E:\\sort\\main.cpp";
	string creation_time = "2014/12/12 12:12:12";
	string last_access_time = "2016/12/12 12:12:12";
	string last_write_time = "2015/12/12 12:12:12";
	string ctime, atime, mtime;
	FileTimeModify::get_file_time(path, ctime, atime, mtime);
	cout << ctime << ' ' << atime << ' ' << mtime << endl;
	/*cout << FileTimeModify::modify_path(path, creation_time, last_access_time, last_write_time) << endl;*/
	return 0;
}
发布了93 篇原创文章 · 获赞 83 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_39942341/article/details/103057820