我的知识记录

如何修改文件夹的修改时间(图文)

修改文件夹的修改时间与修改文件时间类似,但有一些细微差别。以下是不同系统和编程语言的实现方法:

1. 命令行方式

Windows PowerShell:


 
powershell
# 修改文件夹的最后写入时间 $folder = Get-Item "C:\path\to\folder" $folder.LastWriteTime = "2024-01-01 14:30:00" # 修改创建时间 $folder.CreationTime = "2024-01-01 14:30:00" # 修改最后访问时间 $folder.LastAccessTime = "2024-01-01 14:30:00"

Linux/macOS:


 
bash
# 使用 touch 命令修改文件夹时间 touch -m -t 202401011430 /path/to/folder # 修改修改时间 touch -a -t 202401011430 /path/to/folder # 修改访问时间 touch -t 202401011430 /path/to/folder # 同时修改两者 # 示例 touch -m -t 202401011430 /home/user/myfolder

2. 编程语言实现

Python:


 
python
import os from datetime import datetime import platform def change_folder_time(folder_path, new_time): """ 修改文件夹的时间戳 """ if isinstance(new_time, str): new_time = datetime.strptime(new_time, "%Y-%m-%d %H:%M:%S") timestamp = new_time.timestamp() # 修改访问时间和修改时间 os.utime(folder_path, (timestamp, timestamp)) print(f"文件夹 {folder_path} 的时间已修改") # 示例使用 change_folder_time('/path/to/folder', '2024-01-01 14:30:00') # 批量修改子文件夹时间 def batch_change_folder_times(root_path, new_time): """ 递归修改文件夹及其所有子文件夹的时间 """ if isinstance(new_time, str): new_time = datetime.strptime(new_time, "%Y-%m-%d %H:%M:%S") timestamp = new_time.timestamp() # 修改根文件夹 os.utime(root_path, (timestamp, timestamp)) # 递归修改所有子文件夹 for root, dirs, files in os.walk(root_path): for dir_name in dirs: dir_path = os.path.join(root, dir_name) os.utime(dir_path, (timestamp, timestamp)) print(f"已修改: {dir_path}") # 示例使用 batch_change_folder_times('/path/to/root/folder', '2024-01-01 14:30:00')

JavaScript (Node.js):


 
javascript
const fs = require('fs'); const path = require('path'); // 修改单个文件夹时间 function changeFolderTime(folderPath, newDate) { fs.utimes(folderPath, newDate, newDate, (err) => { if (err) { console.error(`修改文件夹时间失败: ${err}`); } else { console.log(`文件夹 ${folderPath} 时间修改成功`); } }); } // 同步版本 function changeFolderTimeSync(folderPath, newDate) { try { fs.utimesSync(folderPath, newDate, newDate); console.log(`文件夹 ${folderPath} 时间修改成功`); } catch (err) { console.error(`修改文件夹时间失败: ${err}`); } } // 递归修改文件夹及所有子项时间 function recursiveChangeTime(folderPath, newDate) { // 修改当前文件夹 changeFolderTimeSync(folderPath, newDate); // 读取文件夹内容 try { const items = fs.readdirSync(folderPath); items.forEach(item => { const itemPath = path.join(folderPath, item); const stats = fs.statSync(itemPath); if (stats.isDirectory()) { // 递归处理子文件夹 recursiveChangeTime(itemPath, newDate); } else { // 修改文件时间 fs.utimesSync(itemPath, newDate, newDate); } }); } catch (err) { console.error(`处理文件夹失败: ${err}`); } } // 示例使用 const newDate = new Date('2024-01-01T14:30:00'); changeFolderTime('./myfolder', newDate); // recursiveChangeTime('./myfolder', newDate); // 递归修改

PHP:


 
php
<?php function changeFolderTime($folderPath, $newTime) { // 转换时间格式 if (is_string($newTime)) { $timestamp = strtotime($newTime); } else { $timestamp = $newTime; } // 修改文件夹时间 if (touch($folderPath, $timestamp)) { echo "文件夹 {$folderPath} 时间修改成功\n"; return true; } else { echo "文件夹 {$folderPath} 时间修改失败\n"; return false; } } // 递归修改文件夹及所有内容的时间 function recursiveChangeFolderTime($folderPath, $newTime) { // 修改当前文件夹 changeFolderTime($folderPath, $newTime); // 获取文件夹内容 $items = scandir($folderPath); foreach ($items as $item) { if ($item === '.' || $item === '..') { continue; } $itemPath = $folderPath . DIRECTORY_SEPARATOR . $item; if (is_dir($itemPath)) { // 递归处理子文件夹 recursiveChangeFolderTime($itemPath, $newTime); } else { // 修改文件时间 touch($itemPath, is_string($newTime) ? strtotime($newTime) : $newTime); } } } // 示例使用 $newTime = '2024-01-01 14:30:00'; changeFolderTime('./myfolder', $newTime); // recursiveChangeFolderTime('./myfolder', $newTime); // 递归修改 ?>

C# (.NET):


 
csharp
using System; using System.IO; class FolderTimeModifier { static void Main() { string folderPath = @"C:\path\to\folder"; DateTime newTime = new DateTime(2024, 1, 1, 14, 30, 0); // 修改文件夹时间 ChangeFolderTime(folderPath, newTime); // 递归修改所有子文件夹和文件 RecursiveChangeTime(folderPath, newTime); } static void ChangeFolderTime(string folderPath, DateTime newTime) { try { DirectoryInfo dirInfo = new DirectoryInfo(folderPath); dirInfo.LastWriteTime = newTime; dirInfo.LastAccessTime = newTime; dirInfo.CreationTime = newTime; Console.WriteLine($"文件夹 {folderPath} 时间修改成功"); } catch (Exception ex) { Console.WriteLine($"修改文件夹时间失败: {ex.Message}"); } } static void RecursiveChangeTime(string folderPath, DateTime newTime) { // 修改当前文件夹 ChangeFolderTime(folderPath, newTime); try { // 获取所有子文件夹 string[] subFolders = Directory.GetDirectories(folderPath); foreach (string subFolder in subFolders) { RecursiveChangeTime(subFolder, newTime); } // 获取所有文件并修改时间 string[] files = Directory.GetFiles(folderPath); foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); fileInfo.LastWriteTime = newTime; fileInfo.LastAccessTime = newTime; fileInfo.CreationTime = newTime; } } catch (Exception ex) { Console.WriteLine($"递归处理失败: {ex.Message}"); } } }

3. 批量处理脚本

Python 批量处理脚本:


 
python
import os import argparse from datetime import datetime def process_folders(folder_list, new_time_str): """ 批量修改多个文件夹的时间 """ new_time = datetime.strptime(new_time_str, "%Y-%m-%d %H:%M:%S") timestamp = new_time.timestamp() for folder_path in folder_list: if os.path.exists(folder_path) and os.path.isdir(folder_path): try: os.utime(folder_path, (timestamp, timestamp)) print(f"✓ 已修改: {folder_path}") except Exception as e: print(f"✗ 修改失败 {folder_path}: {e}") else: print(f"✗ 路径不存在或不是文件夹: {folder_path}") # 命令行使用示例 if __name__ == "__main__": parser = argparse.ArgumentParser(description='修改文件夹时间') parser.add_argument('folders', nargs='+', help='要修改的文件夹路径') parser.add_argument('--time', required=True, help='新时间 (格式: YYYY-MM-DD HH:MM:SS)') args = parser.parse_args() process_folders(args.folders, args.time)

注意事项:

  1. 权限要求: 修改系统文件夹可能需要管理员权限
  2. 递归影响: 递归修改会同时影响文件夹内的所有文件和子文件夹
  3. 系统差异: 不同操作系统对文件夹时间的定义可能略有不同
  4. 备份建议: 修改重要文件夹前建议备份
  5. 应用影响: 时间更改可能影响备份软件、同步工具等的行为
选择适合您需求的方法来修改文件夹的修改时间。
 

如何修改文件夹的修改时间(图文)

标签:

更新时间:2025-12-15 14:55:36

上一篇:文件的创建时间可以修改吗(图文)

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