我的知识记录

怎么修改文件的修改日期(图文)

修改文件的修改日期有多种方法,根据不同操作系统选择合适的方式:

Windows系统

方法1:使用PowerShell(推荐)


 
powershell
# 修改单个文件的最后修改时间 $filePath = "C:\path\to\your\file.txt" (Get-Item $filePath).LastWriteTime = "2023-12-01 15:30:00" # 修改多个文件 Get-ChildItem "C:\folder\*.txt" | ForEach-Object { $_.LastWriteTime = "2023-12-01 15:30:00" } # 同时修改创建时间和修改时间 $file = Get-Item $filePath $file.CreationTime = "2023-12-01 10:00:00" $file.LastWriteTime = "2023-12-01 15:30:00"

方法2:使用命令提示符


 
cmd
# 复制文件以更新时间戳(不改变内容) copy /b filename.txt +,, # 或者使用powershell命令在cmd中执行 powershell "(Get-Item 'filename.txt').LastWriteTime = '2023-12-01 15:30:00'"

方法3:图形界面操作

  1. 右键点击文件 → 属性
  2. 虽然不能直接修改,但可以通过以下方式间接修改:
    • 打开文件并保存(即使没有更改内容)
    • 复制粘贴文件

Linux/Mac系统

方法1:使用touch命令


 
bash
# 修改文件的修改时间和访问时间 touch -d "2023-12-01 15:30:00" filename.txt # 只修改修改时间 touch -m -d "2023-12-01 15:30:00" filename.txt # 只修改访问时间 touch -a -d "2023-12-01 15:30:00" filename.txt # 使用时间戳格式 touch -m -t 202312011530 filename.txt # 修改多个文件 touch -m -d "2023-12-01 15:30:00" *.txt

方法2:使用Python脚本


 
python
import os import time from datetime import datetime # 设置目标时间 target_time = time.mktime(datetime(2023, 12, 1, 15, 30).timetuple()) # 修改文件时间戳 os.utime("filename.txt", (target_time, target_time)) # 批量修改 import glob for file in glob.glob("*.txt"): os.utime(file, (target_time, target_time))

编程方式修改

Python完整示例:


 
python
import os from datetime import datetime import time def change_file_mod_time(filepath, new_datetime_str): """ 修改文件的最后修改时间 :param filepath: 文件路径 :param new_datetime_str: 新的时间字符串,格式如 "2023-12-01 15:30:00" """ try: # 将字符串转换为时间戳 dt = datetime.strptime(new_datetime_str, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(dt.timetuple()) # 修改文件时间戳 os.utime(filepath, (timestamp, timestamp)) print(f"已修改 {filepath} 的最后修改时间为 {new_datetime_str}") return True except Exception as e: print(f"修改失败: {e}") return False # 使用示例 change_file_mod_time("example.txt", "2023-12-01 15:30:00") # 批量修改 def batch_change_mod_time(folder_path, new_datetime_str, file_pattern="*"): import glob import os pattern = os.path.join(folder_path, file_pattern) files = glob.glob(pattern) for file_path in files: if os.path.isfile(file_path): change_file_mod_time(file_path, new_datetime_str) # 批量修改文件夹中所有.txt文件 batch_change_mod_time("./documents", "2023-12-01 15:30:00", "*.txt")

JavaScript (Node.js) 示例:


 
javascript
const fs = require('fs'); function changeFileModTime(filepath, newDate) { // newDate 可以是 Date 对象或时间戳 fs.utimes(filepath, newDate, newDate, (err) => { if (err) { console.error(`修改失败: ${err}`); } else { console.log(`已修改 ${filepath} 的时间戳`); } }); } // 使用示例 const newDate = new Date('2023-12-01 15:30:00'); changeFileModTime('example.txt', newDate); // 批量修改 const fsPromises = require('fs').promises; const path = require('path'); async function batchChangeModTime(dirPath, newDate) { try { const files = await fsPromises.readdir(dirPath); for (const file of files) { const filePath = path.join(dirPath, file); const stat = await fsPromises.stat(filePath); if (stat.isFile()) { await fsPromises.utimes(filePath, newDate, newDate); console.log(`已修改: ${filePath}`); } } } catch (err) { console.error('批量修改失败:', err); } } batchChangeModTime('./documents', newDate);

批量处理工具

Windows批处理脚本:


 
batch
@echo off REM 批量修改文件时间戳 for %%f in (*.txt) do ( copy /b "%%f" +,, echo 已更新 %%f 的时间戳 ) pause

使用第三方工具:

  1. Bulk File Changer(Windows)
  2. Attribute Changer(Windows)
  3. TimeStamp Changer(跨平台)

验证修改结果

Windows PowerShell:


 
powershell
# 查看文件详细信息 Get-Item "filename.txt" | Select-Object Name, CreationTime, LastWriteTime, LastAccessTime # 批量查看 Get-ChildItem "*.txt" | Format-Table Name, LastWriteTime

Linux/Mac终端:


 
bash
# 查看文件详细时间信息 stat filename.txt # 列出文件(显示修改时间) ls -l filename.txt # 批量查看 ls -la *.txt

注意事项:

  1. 权限要求:修改系统文件需要管理员权限
  2. 格式要求:时间格式必须正确
    • Windows PowerShell: "YYYY-MM-DD HH:MM:SS"
    • Linux touch: "YYYY-MM-DD HH:MM:SS" 或 "YYYYMMDDHHMM"
  3. 时区影响:注意时区对时间显示的影响
  4. 系统限制:某些系统或文件系统可能有限制
  5. 备份建议:重要文件修改前建议备份

实用技巧:

  1. 当前时间修改:将文件修改时间设为当前时间
    
     
    powershell
    # Windows (Get-Item "filename.txt").LastWriteTime = Get-Date # Linux/Mac touch filename.txt
  2. 相对时间修改:将文件时间设为几天前
    
     
    powershell
    # Windows PowerShell(3天前) (Get-Item "filename.txt").LastWriteTime = (Get-Date).AddDays(-3) # Linux/Mac(3天前) touch -m -d "3 days ago" filename.txt
推荐使用PowerShell(Windows)或touch命令(Linux/Mac)来修改文件的最后修改时间,这些方法简单易用且效果可靠。对于批量处理或自动化任务,可以使用Python或Node.js脚本。
 

怎么修改文件的修改日期(图文)

标签:

更新时间:2025-12-15 13:52:45

上一篇:上传的文件不能大于1MB(图文)

下一篇:怎么修改文件夹的修改日期(图文)