我的知识记录

怎么修改文档修改时间(图文)

修改文档修改时间是指更改文件系统记录的"最后修改时间",以下是各种操作系统的详细方法:

Windows系统修改方法

1. 使用PowerShell(最推荐)


 
powershell
# 修改单个文件的修改时间 $filePath = "C:\Documents\document.docx" $(Get-Item $filePath).LastWriteTime = "2023-11-15 14:30:00" # 验证修改结果 Get-Item $filePath | Select-Object Name, LastWriteTime

2. 批量修改多个文件


 
powershell
# 修改指定文件夹中所有文档的修改时间 $documentsPath = "C:\Documents" $files = Get-ChildItem -Path $documentsPath -Include *.docx,*.xlsx,*.pdf -Recurse foreach ($file in $files) { $file.LastWriteTime = "2023-11-15 14:30:00" Write-Host "已修改文件时间: $($file.FullName)" }

3. 使用命令提示符(CMD)

需要先安装第三方工具如touch

 
cmd
# 安装touch工具后执行 touch -m -t 202311151430 C:\Documents\document.docx

macOS系统修改方法

使用终端命令


 
bash
# 修改单个文件的修改时间 touch -m -t 202311151430 /Users/username/Documents/document.docx # 格式说明:YYYYMMDDHHMM # 2023年11月15日14点30分

批量修改


 
bash
# 修改文件夹中所有docx文件 find /Users/username/Documents -name "*.docx" -exec touch -m -t 202311151430 {} \;

Linux系统修改方法

使用touch命令


 
bash
# 修改文件修改时间 touch -m -d "2023-11-15 14:30:00" /home/user/documents/document.docx # 或使用时间戳格式 touch -m -t 202311151430.00 /home/user/documents/document.docx

跨平台解决方案

Python脚本(适用于所有系统)


 
python
import os import sys from datetime import datetime def modify_file_mtime(file_path, new_time_str): """ 修改文件的最后修改时间 Args: file_path (str): 文件路径 new_time_str (str): 新的时间,格式如 "2023-11-15 14:30:00" """ try: # 将时间字符串转换为时间戳 dt = datetime.strptime(new_time_str, "%Y-%m-%d %H:%M:%S") timestamp = dt.timestamp() # 修改文件的访问时间和修改时间 # 第一个timestamp是访问时间,第二个是修改时间 os.utime(file_path, (timestamp, timestamp)) print(f"✓ 成功修改文件 '{file_path}' 的修改时间为: {new_time_str}") return True except FileNotFoundError: print(f"✗ 错误: 找不到文件 '{file_path}'") return False except ValueError: print(f"✗ 错误: 时间格式不正确,请使用 'YYYY-MM-DD HH:MM:SS' 格式") return False except PermissionError: print(f"✗ 错误: 没有权限修改文件 '{file_path}'") return False except Exception as e: print(f"✗ 错误: {e}") return False def batch_modify_mtime(directory, file_extensions, new_time_str): """ 批量修改目录中文件的修改时间 Args: directory (str): 目录路径 file_extensions (list): 文件扩展名列表 new_time_str (str): 新的时间字符串 """ import glob if not os.path.exists(directory): print(f"✗ 错误: 目录 '{directory}' 不存在") return # 构建搜索模式 patterns = [os.path.join(directory, f"**/*{ext}") for ext in file_extensions] modified_count = 0 for pattern in patterns: files = glob.glob(pattern, recursive=True) for file_path in files: if modify_file_mtime(file_path, new_time_str): modified_count += 1 print(f"\n总共修改了 {modified_count} 个文件") # 使用示例 if __name__ == "__main__": # 修改单个文件 modify_file_mtime("document.docx", "2023-11-15 14:30:00") # 批量修改 extensions = [".docx", ".xlsx", ".pdf", ".txt"] batch_modify_mtime("C:/Documents", extensions, "2023-11-15 14:30:00")

使用第三方工具

Windows推荐工具

  1. Attribute Changer
    • 图形界面操作
    • 支持批量处理
    • 可以同时修改创建时间、访问时间、修改时间
  2. BulkFileChanger
    • 免费工具
    • 支持多种时间属性
    • 可以按条件筛选文件
  3. NewFileTime
    • 简单易用
    • 支持拖拽操作
    • 可以从其他文件复制时间属性

高级用法

1. 复制其他文件的时间属性


 
powershell
# Windows PowerShell $sourceFile = Get-Item "source.docx" $targetFile = Get-Item "target.docx" $targetFile.LastWriteTime = $sourceFile.LastWriteTime

 
bash
# macOS/Linux touch -r source.docx target.docx

2. 设置为当前时间


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

 
bash
# macOS/Linux touch document.docx

实际应用示例

场景1:整理照片文件时间


 
python
# 修改照片文件时间为拍摄时间(假设文件名包含日期) import os from datetime import datetime def modify_photo_time_by_filename(photo_path): filename = os.path.basename(photo_path) # 假设文件名格式为 IMG_20231115_143000.jpg if "IMG_" in filename and len(filename) >= 19: try: date_part = filename[4:19] # 提取 20231115_143000 date_str = date_part.replace("_", " ") dt = datetime.strptime(date_str, "%Y%m%d %H%M%S") timestamp = dt.timestamp() os.utime(photo_path, (timestamp, timestamp)) print(f"修改 {filename} 时间成功") except: print(f"无法解析 {filename} 的时间") # 批量处理照片 photos_dir = "/path/to/photos" for photo in os.listdir(photos_dir): if photo.lower().endswith(('.jpg', '.jpeg', '.png')): modify_photo_time_by_filename(os.path.join(photos_dir, photo))

注意事项

  1. 管理员权限:某些情况下需要管理员权限才能修改系统文件时间
  2. 文件锁定:确保文件没有被其他程序占用
  3. 备份重要文件:修改前建议备份重要文件
  4. 时间格式:严格按照要求的时间格式输入
  5. 云同步影响:修改本地文件时间后,云同步服务可能会覆盖这些更改
  6. 合法用途:请仅在合法合理的场景下使用此功能
选择最适合您需求和操作系统的方法来修改文档修改时间。对于偶尔使用,推荐使用PowerShell或终端命令;对于频繁批量操作,建议使用Python脚本。
 

怎么修改文档修改时间(图文)

标签:

更新时间:2025-12-18 10:04:28

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

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