文档上传日期 怎么修改文档的修改日期和时间怎么修改文档的修改日期和时间
文档上传日期修改方法
1. Windows 系统修改文档上传日期
PowerShell 方法:
powershell
# 修改单个文档的上传日期(修改时间) (Get-Item "report.pdf").LastWriteTime = "2024-09-20 14:30:00" # 批量修改多个文档类型 Get-ChildItem *.pdf,*.doc,*.docx,*.txt | ForEach-Object { $_.LastWriteTime = "2024-09-20 14:30:00" Write-Output "已设置上传日期: $($_.Name)" } # 根据当前日期设置上传时间 Get-ChildItem *.pdf | ForEach-Object { $_.LastWriteTime = Get-Date Write-Output "已设置为今天: $($_.Name)" }命令提示符方法:
cmd
# 使用 copy 命令更新时间为当前时间 copy /b report.pdf+,, >nul # 使用 PowerShell 命令(在 CMD 中) powershell "(Get-Item 'report.pdf').LastWriteTime = '2024-09-20 14:30:00'"2. Linux/Unix/Mac 系统修改文档上传日期
使用 touch 命令:
bash
# 修改单个文档的上传日期 touch -d "2024-09-20 14:30:00" report.pdf # 批量修改多种文档类型 touch -d "2024-09-20 14:30:00" *.pdf *.doc *.docx *.txt # 修改为当前时间 touch report.pdf # 批量修改为当前时间 touch *.pdf *.doc *.docx文档的修改日期和时间修改方法
1. 完整的文件时间属性修改
Windows PowerShell:
powershell
# 修改文档的所有时间属性 $doc = Get-Item "document.pdf" $uploadDateTime = "2024-09-20 14:30:00" # 修改最后写入时间(最重要) $doc.LastWriteTime = $uploadDateTime # 修改创建时间 $doc.CreationTime = $uploadDateTime # 修改最后访问时间 $doc.LastAccessTime = $uploadDateTime Write-Output "文档时间已全部更新为: $uploadDateTime"Linux/Unix:
bash
# 修改修改时间 (-m) 和访问时间 (-a) touch -m -d "2024-09-20 14:30:00" document.pdf # 修改时间 touch -a -d "2024-09-20 14:30:00" document.pdf # 访问时间 # 同时修改两种时间 touch -d "2024-09-20 14:30:00" document.pdf # 使用时间戳格式 touch -t 202409201430 document.pdf2. Python 脚本批量修改
python
import os import glob from datetime import datetime import time class DocumentTimeModifier: def __init__(self, target_datetime): """ 初始化文档时间修改器 :param target_datetime: 目标日期时间字符串,如 '2024-09-20 14:30:00' """ self.target_datetime = datetime.strptime(target_datetime, '%Y-%m-%d %H:%M:%S') self.timestamp = time.mktime(self.target_datetime.timetuple()) def modify_single_document(self, file_path): """修改单个文档的时间""" try: if os.path.isfile(file_path): os.utime(file_path, (self.timestamp, self.timestamp)) return True, f"成功修改: {file_path}" else: return False, f"文件不存在: {file_path}" except Exception as e: return False, f"修改失败 {file_path}: {str(e)}" def modify_batch_documents(self, patterns, recursive=False): """批量修改文档时间""" results = [] for pattern in patterns: if recursive: files = [] for root, dirs, filenames in os.walk('.'): for filename in filenames: if self._matches_pattern(filename, pattern): files.append(os.path.join(root, filename)) else: files = glob.glob(pattern) for file_path in files: success, message = self.modify_single_document(file_path) results.append({ 'file': file_path, 'success': success, 'message': message }) return results def _matches_pattern(self, filename, pattern): """检查文件名是否匹配模式""" if pattern.startswith('*.'): extension = pattern[1:] # 包含点号 return filename.lower().endswith(extension.lower()) return filename == pattern # 使用示例 modifier = DocumentTimeModifier('2024-09-20 14:30:00') # 修改单个文档 success, message = modifier.modify_single_document('report.pdf') print(message) # 批量修改文档 document_patterns = ['*.pdf', '*.doc', '*.docx', '*.txt', '*.xlsx'] results = modifier.modify_batch_documents(document_patterns) # 显示结果 for result in results: if result['success']: print(f"✓ {result['message']}") else: print(f"✗ {result['message']}")3. 高级批量处理脚本
bash
#!/bin/bash # 文档时间批量修改脚本 DOCUMENT_TYPES=("*.pdf" "*.doc" "*.docx" "*.txt" "*.xlsx" "*.ppt" "*.pptx") # 设置目标上传日期时间 UPLOAD_DATE="2024-09-20 14:30:00" echo "开始修改文档上传日期为: $UPLOAD_DATE" # 遍历每种文档类型 for doc_type in "${DOCUMENT_TYPES[@]}"; do count=0 for file in $doc_type; do if [ -f "$file" ]; then touch -m -d "$UPLOAD_DATE" "$file" echo "✓ 已修改: $file" ((count++)) fi done echo "修改了 $count 个 $doc_type 文件" done echo "所有文档上传日期修改完成!"4. 按文件夹结构批量修改
python
import os from datetime import datetime import time def modify_documents_in_folders(root_directory, target_datetime, doc_extensions=None): """ 递归修改文件夹中所有文档的时间 :param root_directory: 根目录路径 :param target_datetime: 目标日期时间 :param doc_extensions: 文档扩展名列表 """ if doc_extensions is None: doc_extensions = ['.pdf', '.doc', '.docx', '.txt', '.xlsx', '.ppt', '.pptx'] target_dt = datetime.strptime(target_datetime, '%Y-%m-%d %H:%M:%S') timestamp = time.mktime(target_dt.timetuple()) modified_count = 0 error_count = 0 for root, dirs, files in os.walk(root_directory): for file in files: # 检查文件扩展名 _, ext = os.path.splitext(file) if ext.lower() in doc_extensions: file_path = os.path.join(root, file) try: os.utime(file_path, (timestamp, timestamp)) print(f"✓ 已修改: {file_path}") modified_count += 1 except Exception as e: print(f"✗ 修改失败: {file_path} - {str(e)}") error_count += 1 print(f"\n总计: 修改了 {modified_count} 个文档,{error_count} 个失败") # 使用示例 modify_documents_in_folders('./documents', '2024-09-20 14:30:00')5. 验证修改结果
Windows PowerShell 验证:
powershell
# 查看文档的详细时间信息 Get-ChildItem *.pdf | Select-Object Name, LastWriteTime, CreationTime | Format-Table -AutoSize # 验证特定文档的时间 (Get-Item "report.pdf").LastWriteTimeLinux/Unix 验证:
bash
# 查看文档详细时间信息 stat report.pdf # 查看多个文档的时间 ls -la *.pdf *.doc *.docx注意事项:
- 权限要求:确保有足够权限修改目标文件
- 文件锁定:确保文件未被其他程序占用
- 备份重要文件:操作前建议备份重要文档
- 时间格式:注意不同系统支持的时间格式略有差异
- 批量操作谨慎:大量文件操作时建议先在小范围内测试

更新时间:2025-12-13 17:16:05
上一篇:文档上传日期修改怎么弄 修改了文件的修改日期后还能用吗
下一篇:文档上传日期修改不了什么原因呢
