我的知识记录

如何改变文档的修改时间(图文)

改变文档的修改时间有多种方法,以下是不同操作系统下的具体操作:

Windows系统

方法1:使用PowerShell(推荐)


 
powershell
# 修改单个文件的修改时间 $filePath = "C:\path\to\your\document.txt" $newModificationTime = Get-Date "2023-12-25 15:30:00" $(Get-Item $filePath).LastWriteTime = $newModificationTime # 验证修改是否成功 (Get-Item $filePath).LastWriteTime

方法2:使用CMD命令


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

方法3:使用第三方工具


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

Linux/macOS系统

使用touch命令


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

跨平台编程解决方案

Python脚本


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

JavaScript (Node.js)


 
javascript
const fs = require('fs'); function changeModificationTime(filePath, newDateTimeStr) { // 将日期字符串转换为时间对象 const newDate = new Date(newDateTimeStr); // 获取当前文件状态 fs.stat(filePath, (err, stats) => { if (err) { console.error('无法读取文件状态:', err); return; } // 修改文件时间戳(保持访问时间不变) fs.utimes(filePath, stats.atime, newDate, (err) => { if (err) { console.error('修改时间失败:', err); } else { console.log(`成功修改 ${filePath} 的修改时间为 ${newDateTimeStr}`); } }); }); } // 使用示例 changeModificationTime('document.txt', '2023-12-25 15:30:00');

批量修改多个文件

PowerShell批量操作


 
powershell
# 批量修改指定目录下所有文档的修改时间 $folderPath = "C:\Documents" $filter = "*.txt" # 可以改为 "*.*" 表示所有文件 $newModTime = Get-Date "2023-12-25 15:30:00" Get-ChildItem -Path $folderPath -Filter $filter | ForEach-Object { $_.LastWriteTime = $newModTime Write-Host "已修改: $($_.Name)" }

Linux/macOS批量操作


 
bash
# 修改当前目录下所有.txt文件的修改时间 find . -name "*.txt" -exec touch -m -d "2023-12-25 15:30:00" {} \; # 或者使用循环方式 for file in *.txt *.pdf *.docx; do [ -f "$file" ] && touch -m -d "2023-12-25 15: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 - 使用-r参数参考另一个文件的时间 touch -m -r reference.docx target.txt

2. 将修改时间设置为当前时间


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

 
bash
# Linux/macOS touch -m document.txt

3. 递归修改整个目录


 
powershell
# Windows PowerShell - 递归修改目录下所有文件 Get-ChildItem -Path "C:\Documents" -Recurse -File | ForEach-Object { $_.LastWriteTime = Get-Date "2023-12-25 15:30:00" }

 
bash
# Linux/macOS - 递归修改目录下所有文件 find /path/to/directory -type f -exec touch -m -d "2023-12-25 15:30:00" {} \;

验证修改结果

PowerShell验证


 
powershell
# 查看文件详细信息 Get-Item "document.txt" | Select-Object Name, LastWriteTime, CreationTime, LastAccessTime

Linux/macOS验证


 
bash
# 查看文件时间信息 stat document.txt # 或者 ls -l document.txt

注意事项

  1. 权限问题:确保对目标文件有写入权限
  2. 备份重要文件:操作前建议备份重要文档
  3. 时间格式:不同系统接受的时间格式可能不同
  4. 同步软件影响:云同步软件可能会覆盖手动修改的时间戳
  5. 文件系统限制:某些文件系统对时间精度支持不同(如FAT32精确到2秒)
选择适合您操作系统和需求的方法即可轻松修改文档的修改时间。
 

如何改变文档的修改时间(图文)

标签:

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

上一篇:如何修改文档编辑时间(图文)

下一篇:百度文库上传时间修改不了(图文)