怎么修改文件夹的修改日期(图文)
修改文件夹的修改日期与修改文件的方法类似,但在不同操作系统中有细微差别。以下是各种系统的操作方法:
Windows系统
方法1:使用PowerShell(推荐)
powershell
# 修改单个文件夹的最后修改时间 $folderPath = "C:\path\to\your\folder" (Get-Item $folderPath).LastWriteTime = "2023-12-01 15:30:00" # 修改多个文件夹 Get-ChildItem "C:\parent_folder" -Directory | ForEach-Object { $_.LastWriteTime = "2023-12-01 15:30:00" } # 同时修改创建时间和修改时间 $folder = Get-Item $folderPath $folder.CreationTime = "2023-12-01 10:00:00" $folder.LastWriteTime = "2023-12-01 15:30:00"方法2:通过创建/删除文件触发
powershell
# 创建临时文件来更新文件夹时间戳 $folderPath = "C:\path\to\folder" $tempFile = Join-Path $folderPath "temp.txt" New-Item -ItemType File -Path $tempFile Remove-Item $tempFileLinux/Mac系统
方法1:使用touch命令
bash
# 修改文件夹的修改时间 touch -m -d "2023-12-01 15:30:00" folder_name # 递归修改文件夹及其内容的时间 touch -m -d "2023-12-01 15:30:00" folder_name/* touch -m -d "2023-12-01 15:30:00" folder_name # 使用时间戳格式 touch -m -t 202312011530 folder_name方法2:通过创建/删除文件触发
bash
# 创建并删除临时文件来更新文件夹时间戳 touch folder_name/temp_file && rm folder_name/temp_file编程方式修改
Python示例:
python
import os import time from datetime import datetime def change_folder_mod_time(folder_path, new_datetime_str): """ 修改文件夹的最后修改时间 :param folder_path: 文件夹路径 :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(folder_path, (timestamp, timestamp)) print(f"已修改 {folder_path} 的最后修改时间为 {new_datetime_str}") return True except Exception as e: print(f"修改失败: {e}") return False # 使用示例 change_folder_mod_time("./my_folder", "2023-12-01 15:30:00") # 批量修改多个文件夹 def batch_change_folder_mod_time(parent_path, new_datetime_str): import glob import os folders = [f for f in glob.glob(os.path.join(parent_path, "*")) if os.path.isdir(f)] for folder_path in folders: change_folder_mod_time(folder_path, new_datetime_str) # 批量修改父文件夹中的所有子文件夹 batch_change_folder_mod_time("./parent_folder", "2023-12-01 15:30:00")JavaScript (Node.js) 示例:
javascript
const fs = require('fs').promises; const path = require('path'); async function changeFolderModTime(folderPath, newDate) { try { // 修改文件夹时间戳 await fs.utimes(folderPath, newDate, newDate); console.log(`已修改 ${folderPath} 的时间戳`); return true; } catch (err) { console.error(`修改失败: ${err}`); return false; } } // 使用示例 const newDate = new Date('2023-12-01 15:30:00'); changeFolderModTime('./my_folder', newDate); // 批量修改 async function batchChangeFolderModTime(parentPath, newDate) { try { const items = await fs.readdir(parentPath); for (const item of items) { const itemPath = path.join(parentPath, item); const stat = await fs.stat(itemPath); if (stat.isDirectory()) { await changeFolderModTime(itemPath, newDate); } } } catch (err) { console.error('批量修改失败:', err); } } batchChangeFolderModTime('./parent_folder', newDate);通过文件操作间接修改
Windows PowerShell:
powershell
# 通过在文件夹中创建和删除文件来更新时间戳 function Update-FolderTimestamp { param( [string]$FolderPath, [datetime]$NewTime = (Get-Date) ) $tempFile = Join-Path $FolderPath "temp_timestamp_update.txt" try { # 创建临时文件 New-Item -ItemType File -Path $tempFile -Force | Out-Null # 设置临时文件的时间戳 (Get-Item $tempFile).LastWriteTime = $NewTime # 删除临时文件,这会更新文件夹的时间戳 Remove-Item $tempFile -Force # 再次设置文件夹时间戳确保准确性 (Get-Item $FolderPath).LastWriteTime = $NewTime Write-Host "已更新文件夹时间戳: $FolderPath" } catch { Write-Error "更新失败: $_" } } # 使用示例 Update-FolderTimestamp -FolderPath "C:\MyFolder" -NewTime "2023-12-01 15:30:00"Linux/Mac Shell脚本:
bash
#!/bin/bash update_folder_timestamp() { local folder_path="$1" local timestamp="$2" if [ ! -d "$folder_path" ]; then echo "错误: $folder_path 不是一个有效的文件夹" return 1 fi # 创建临时文件 local temp_file="$folder_path/.timestamp_temp" touch "$temp_file" # 设置临时文件时间戳 if [ -n "$timestamp" ]; then touch -m -d "$timestamp" "$temp_file" fi # 删除临时文件 rm "$temp_file" # 如果指定了时间,也设置文件夹时间 if [ -n "$timestamp" ]; then touch -m -d "$timestamp" "$folder_path" fi echo "已更新文件夹时间戳: $folder_path" } # 使用示例 update_folder_timestamp "./my_folder" "2023-12-01 15:30:00"验证修改结果
Windows PowerShell:
powershell
# 查看文件夹详细信息 Get-Item "folder_name" | Select-Object Name, CreationTime, LastWriteTime, LastAccessTime # 查看多个文件夹 Get-ChildItem -Directory | Format-Table Name, LastWriteTimeLinux/Mac终端:
bash
# 查看文件夹详细时间信息 stat folder_name # 列出文件夹(显示修改时间) ls -ld folder_name # 查看多个文件夹 ls -ld */注意事项:
- 权限要求:修改系统文件夹需要管理员权限
- 文件夹行为差异:
- 在Windows中,文件夹的修改时间会在其中的文件被修改时自动更新
- 在Linux中,文件夹时间戳的行为可能因文件系统而异
- 递归影响:修改文件夹时间戳通常不影响其内部文件的时间戳
- 格式要求:时间格式必须正确
- 备份建议:重要文件夹修改前建议备份
实用技巧:
- 更新为当前时间:
powershell
# Windows (Get-Item "folder_name").LastWriteTime = Get-Date # Linux/Mac touch folder_name - 批量更新多个文件夹:
powershell
# Windows PowerShell Get-ChildItem -Directory | ForEach-Object { $_.LastWriteTime = "2023-12-01 15:30:00" } # Linux/Mac for dir in */; do touch -m -d "2023-12-01 15:30:00" "$dir" done

更新时间:2025-12-15 13:53:05
上一篇:怎么修改文件的修改日期(图文)
