我的知识记录

如何修改文档编辑时间(图文)

修改文档编辑时间(即文件的最后修改时间)有多种方法,以下是在不同操作系统下的具体操作:

Windows系统

方法1:使用PowerShell(推荐)


 
powershell
# 修改单个文件的编辑时间 $filePath = "C:\path\to\your\document.txt" $newEditTime = Get-Date "2023-12-25 14:30:00" $(Get-Item $filePath).LastWriteTime = $newEditTime # 验证修改结果 (Get-Item $filePath).LastWriteTime

方法2:使用CMD命令


 
cmd
# 使用copy命令更新文件时间戳(不改变内容) copy /b filename.txt+,, # 这会将文件的修改时间更新为当前时间

方法3:使用第三方工具


 
cmd
# 使用nircmd工具(需要下载安装) nircmd.exe setfiletime "document.txt" "25-12-2023 14:30:00" "25-12-2023 14:30:00"

Linux/macOS系统

使用touch命令


 
bash
# 修改文件的修改时间(编辑时间) touch -m -t 202312251430.00 document.txt # 使用更直观的-d参数 touch -m -d "2023-12-25 14:30:00" document.txt # 同时修改访问时间和修改时间 touch -a -m -d "2023-12-25 14:30:00" document.txt

跨平台编程方法

Python脚本


 
python
import os import time from datetime import datetime def change_edit_time(filepath, new_datetime_str): """ 修改文件的编辑时间 参数: filepath: 文件路径 new_datetime_str: 新的日期时间字符串,格式如 "2023-12-25 14:30:00" """ try: # 将字符串转换为时间戳 new_timestamp = time.mktime( datetime.strptime(new_datetime_str, "%Y-%m-%d %H:%M:%S").timetuple() ) # 获取当前文件状态 current_stat = os.stat(filepath) # 修改文件时间戳(保持访问时间不变,只修改编辑时间) os.utime(filepath, (current_stat.st_atime, new_timestamp)) print(f"成功将 {filepath} 的编辑时间修改为 {new_datetime_str}") return True except Exception as e: print(f"修改失败: {e}") return False # 使用示例 change_edit_time("document.txt", "2023-12-25 14:30:00") # 验证修改结果 def check_file_times(filepath): stat = os.stat(filepath) print(f"创建时间: {datetime.fromtimestamp(stat.st_ctime)}") print(f"修改时间: {datetime.fromtimestamp(stat.st_mtime)}") print(f"访问时间: {datetime.fromtimestamp(stat.st_atime)}") check_file_times("document.txt")

JavaScript (Node.js)


 
javascript
const fs = require('fs'); function changeEditTime(filePath, newDateTimeStr) { // 将日期字符串转换为毫秒时间戳 const newTimestamp = new Date(newDateTimeStr).getTime(); // 获取当前文件状态 fs.stat(filePath, (err, stats) => { if (err) { console.error('读取文件状态失败:', err); return; } // 修改文件时间戳 fs.utimes(filePath, stats.atime, new Date(newTimestamp), (err) => { if (err) { console.error('修改文件时间失败:', err); } else { console.log(`成功将 ${filePath} 的编辑时间修改为 ${newDateTimeStr}`); } }); }); } // 使用示例 changeEditTime('document.txt', '2023-12-25 14:30:00');

批量修改多个文件

PowerShell批量修改


 
powershell
# 批量修改文件夹中所有文档的编辑时间 $folderPath = "C:\Documents" $documents = Get-ChildItem $folderPath -Include *.docx,*.pdf,*.txt -Recurse $newEditTime = Get-Date "2023-12-25 14:30:00" foreach ($doc in $documents) { $doc.LastWriteTime = $newEditTime Write-Host "已修改: $($doc.Name)" }

Linux/macOS批量修改


 
bash
# 修改当前目录下所有.txt文件的编辑时间 find . -name "*.txt" -exec touch -m -d "2023-12-25 14:30:00" {} \; # 或者使用循环 for file in *.txt *.pdf *.docx; do [ -f "$file" ] && touch -m -d "2023-12-25 14:30:00" "$file" && echo "已修改: $file" done

高级技巧

1. 根据另一个文件设置时间


 
powershell
# Windows PowerShell $referenceFile = Get-Item "reference.docx" $targetFile = Get-Item "target.txt" $targetFile.LastWriteTime = $referenceFile.LastWriteTime

 
bash
# Linux/macOS touch -m -r reference.docx target.txt

2. 将编辑时间设置为当前时间


 
powershell
# Windows $(Get-Item "document.txt").LastWriteTime = Get-Date

 
bash
# Linux/macOS touch -m document.txt

注意事项

  1. 权限要求:确保对目标文件有写入权限
  2. 备份重要文件:操作前建议备份重要文档
  3. 时间格式:注意不同系统接受的时间格式可能不同
  4. 同步软件影响:云同步软件可能会覆盖手动修改的时间
  5. 文件系统限制:某些文件系统对时间精度有不同的支持
选择适合您操作系统的方法即可完成文档编辑时间的修改。
 

如何修改文档编辑时间(图文)

标签:

更新时间:2025-12-18 10:36:20

上一篇:怎么更改文档时间编辑时间显示(图文)

下一篇:如何改变文档的修改时间(图文)