文件夹里文件修改日期怎么改(图文)
修改文件夹内文件的修改日期有多种方法,以下是不同操作系统下的详细操作指南:
Windows 系统
方法一:使用 PowerShell(推荐)
修改单个文件的修改日期
powershell
# 修改指定文件的修改日期 $file = Get-Item "C:\path\to\folder\document.txt" $file.LastWriteTime = "2023-08-15 14:30:00"批量修改所有文件的修改日期
powershell
# 修改文件夹内所有文件的修改日期 Get-ChildItem "C:\path\to\folder" -File | ForEach-Object { $_.LastWriteTime = "2023-08-15 14:30:00" }修改特定类型文件的修改日期
powershell
# 只修改txt文件的修改日期 Get-ChildItem "C:\path\to\folder\*.txt" | ForEach-Object { $_.LastWriteTime = "2023-08-15 14:30:00" } # 修改多种类型文件 Get-ChildItem "C:\path\to\folder" -Include *.txt,*.doc,*.pdf | ForEach-Object { $_.LastWriteTime = "2023-08-15 14:30:00" }递归修改子文件夹中的文件
powershell
# 修改文件夹及其子文件夹中所有文件的修改日期 Get-ChildItem "C:\path\to\folder" -Recurse -File | ForEach-Object { $_.LastWriteTime = "2023-08-15 14:30:00" }方法二:使用 CMD 命令
cmd
# 通过 PowerShell 命令修改 powershell "Get-ChildItem 'C:\path\to\folder' -File | %%{$_.LastWriteTime = '2023-08-15 14:30:00'}"macOS 系统
使用 Terminal 终端
修改单个文件
bash
# 修改单个文件的修改时间 touch -d "2023-08-15 14:30:00" /path/to/folder/document.txt批量修改文件
bash
# 修改文件夹内所有文件的修改时间 cd /path/to/folder touch -d "2023-08-15 14:30:00" * # 修改特定类型文件 touch -d "2023-08-15 14:30:00" *.txt # 使用 find 命令修改所有文件 find /path/to/folder -type f -exec touch -d "2023-08-15 14:30:00" {} \;Linux 系统
使用 Bash 命令
修改单个文件
bash
# 修改单个文件的修改时间 touch -d "2023-08-15 14:30:00" /path/to/folder/document.txt批量修改文件
bash
# 修改文件夹内所有文件 cd /path/to/folder touch -d "2023-08-15 14:30:00" * # 使用 find 命令修改所有文件 find /path/to/folder -type f -exec touch -d "2023-08-15 14:30:00" {} \; # 递归修改包括子目录中的文件 find /path/to/folder -type f -exec touch -d "2023-08-15 14:30:00" {} +编程方式实现
Python 脚本
基础版本
python
import os import time from datetime import datetime from pathlib import Path def change_files_modification_date(folder_path, new_date_string, file_pattern="*"): """ 修改文件夹内文件的修改日期 :param folder_path: 文件夹路径 :param new_date_string: 新的日期时间字符串,如 "2023-08-15 14:30:00" :param file_pattern: 文件模式,如 "*.txt" 只修改txt文件 """ folder = Path(folder_path) # 解析日期时间 try: new_datetime = datetime.strptime(new_date_string, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(new_datetime.timetuple()) except ValueError: print("错误: 日期格式不正确,请使用 'YYYY-MM-DD HH:MM:SS' 格式") return # 查找匹配的文件 if "*" in file_pattern: files = list(folder.glob(file_pattern)) else: files = [f for f in folder.iterdir() if f.is_file()] modified_count = 0 for file_path in files: try: os.utime(str(file_path), (timestamp, timestamp)) print(f"✓ 已修改: {file_path.name}") modified_count += 1 except Exception as e: print(f"✗ 修改失败 {file_path.name}: {e}") print(f"共修改了 {modified_count} 个文件") # 使用示例 change_files_modification_date("./documents", "2023-08-15 14:30:00") change_files_modification_date("./documents", "2023-08-15 14:30:00", "*.txt")高级版本(支持递归和更多选项)
python
import os import time import argparse from datetime import datetime from pathlib import Path import fnmatch class FolderFileDateModifier: def __init__(self, folder_path): self.folder_path = Path(folder_path) if not self.folder_path.exists(): raise FileNotFoundError(f"文件夹不存在: {folder_path}") def modify_dates(self, new_date_string, pattern="*", recursive=False, include_subdirs=False, dry_run=False): """ 修改文件夹内文件的修改日期 :param new_date_string: 新的日期时间字符串 :param pattern: 文件匹配模式 :param recursive: 是否递归处理子目录 :param include_subdirs: 是否也修改子目录的时间 :param dry_run: 是否只预览不实际修改 """ # 解析日期时间 try: new_datetime = datetime.strptime(new_date_string, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(new_datetime.timetuple()) except ValueError: print("错误: 日期格式不正确,请使用 'YYYY-MM-DD HH:MM:SS' 格式") return # 查找文件 files_to_modify = self._find_files(pattern, recursive) print(f"找到 {len(files_to_modify)} 个文件") if dry_run: print("【预览模式】以下文件将被修改:") for file_path in files_to_modify: print(f" {file_path}") return # 确认操作 if files_to_modify: confirm = input(f"确认修改这 {len(files_to_modify)} 个文件的修改日期吗? (y/N): ") if confirm.lower() != 'y': print("操作已取消") return # 执行修改 modified_count = 0 for file_path in files_to_modify: try: os.utime(str(file_path), (timestamp, timestamp)) print(f"✓ 已修改: {file_path.relative_to(self.folder_path)}") modified_count += 1 except Exception as e: print(f"✗ 修改失败 {file_path.name}: {e}") # 如果需要,也修改子目录的时间 if include_subdirs and recursive: self._modify_directory_dates(timestamp, recursive) print(f"共修改了 {modified_count} 个文件") def _find_files(self, pattern, recursive): """查找匹配的文件""" files = [] if recursive: items = self.folder_path.rglob("*") else: items = self.folder_path.iterdir() for item in items: if item.is_file() and fnmatch.fnmatch(item.name, pattern): files.append(item) return files def _modify_directory_dates(self, timestamp, recursive): """修改目录的时间""" if recursive: dirs = [self.folder_path] + list(self.folder_path.rglob("*")) dirs = [d for d in dirs if d.is_dir()] else: dirs = [d for d in self.folder_path.iterdir() if d.is_dir()] for dir_path in dirs: try: os.utime(str(dir_path), (timestamp, timestamp)) print(f"✓ 已修改目录时间: {dir_path.relative_to(self.folder_path)}") except Exception as e: print(f"✗ 修改目录失败 {dir_path.name}: {e}") # 使用示例 if __name__ == "__main__": # 创建修改器实例 modifier = FolderFileDateModifier("./my_documents") # 修改所有文件 modifier.modify_dates("2023-08-15 14:30:00") # 只修改txt文件 modifier.modify_dates("2023-08-15 14:30:00", pattern="*.txt") # 递归修改所有子目录中的文件 modifier.modify_dates("2023-08-15 14:30:00", recursive=True) # 预览模式(不实际修改) modifier.modify_dates("2023-08-15 14:30:00", dry_run=True)第三方工具推荐
Windows 工具
- BulkFileChanger - NirSoft出品的免费批量文件属性修改工具
- Attribute Changer - 功能强大的文件属性修改工具
- Advanced Renamer - 支持批量修改文件时间属性
跨平台工具
- Total Commander - 文件管理器,内置批量时间修改功能
- FreeCommander - 免费的文件管理工具
实际应用示例
示例1:整理照片文件
powershell
# Windows PowerShell: 将照片文件夹中所有图片文件的修改日期设为拍摄日期 Get-ChildItem "C:\Photos\*.jpg" | ForEach-Object { $_.LastWriteTime = "2023-06-15 10:30:00" }示例2:项目文件时间统一
bash
# macOS/Linux: 统一项目文件的修改时间 cd /path/to/project find . -name "*.py" -o -name "*.js" -o -name "*.html" | xargs touch -d "2023-08-15 14:30:00"示例3:文档归档处理
python
# Python: 归档文档并统一修改时间 def archive_documents(source_folder, archive_date): """归档文档并设置统一的修改时间""" modifier = FolderFileDateModifier(source_folder) modifier.modify_dates(archive_date, pattern="*.doc*") # 使用示例 # archive_documents("./reports_2023", "2023-12-31 23:59:59")验证修改结果
Windows 验证
powershell
# 查看文件修改时间 Get-ChildItem "C:\path\to\folder" | Select-Object Name, LastWriteTime # 按修改时间排序查看 Get-ChildItem "C:\path\to\folder" | Sort-Object LastWriteTimemacOS/Linux 验证
bash
# 查看文件详细信息 ls -la /path/to/folder/ # 按修改时间排序 ls -lt /path/to/folder/ # 使用 stat 命令查看详细时间信息 stat /path/to/folder/*注意事项
- 权限要求:确保对目标文件夹和文件有写入权限
- 备份重要文件:批量修改前建议备份重要数据
- 时间格式:严格按照指定格式输入日期时间
- 系统限制:
- Windows 完全支持修改创建时间和修改时间
- macOS 支持修改创建时间
- Linux 大多数文件系统不保存创建时间
- 递归操作:递归修改时要特别小心,避免误操作
- 预览模式:建议先使用预览模式确认要修改的文件
- 确认机制:添加用户确认步骤防止意外批量修改

更新时间:2025-12-17 12:49:09
上一篇:文件创建时间晚于修改时间(图文)
下一篇:文件夹怎么修改日期和时间(图文)
