本文目录导读:

- Windows PowerShell 脚本 - 添加NTFS文件摘要信息
- Linux Bash 脚本 - 使用扩展属性
- Python 跨平台脚本(推荐)
- 简单实用的BAT脚本(Windows)
- 使用说明
- 注意事项
这里我为您提供几个不同场景的批量添加文件描述信息的脚本,包括Windows和Linux系统:
Windows PowerShell 脚本 - 添加NTFS文件摘要信息
# batch-add-file-description.ps1
# 为当前目录下所有.txt文件添加描述
$description = "这是自动添加的描述信息"
$targetPath = "C:\Your\Target\Directory"
# 遍历目标文件并添加描述
Get-ChildItem -Path $targetPath -Filter "*.txt" | ForEach-Object {
try {
# 获取文件对象
$file = $_.FullName
$shell = New-Object -ComObject Shell.Application
$shellfolder = $shell.Namespace((Get-Item $file).DirectoryName)
$shellfile = $shellfolder.ParseName((Get-Item $file).Name)
# 添加描述信息(索引为 0 通常是名称,5 是描述)
$shellfolder.GetDetailsOf($shellfile, 5) # 获取当前描述
$shellfile.ExtendedProperty("System.Comment") = $description
Write-Host "已添加描述: $file" -ForegroundColor Green
}
catch {
Write-Host "处理文件失败: $file" -ForegroundColor Red
Write-Host "错误: $_"
}
}
Write-Host "完成!" -ForegroundColor Yellow
Linux Bash 脚本 - 使用扩展属性
#!/bin/bash
# batch-add-description.sh
# 定义目标目录和描述信息
TARGET_DIR="/path/to/your/files"
DESCRIPTION="这是文件描述信息"
# 检查目录是否存在
if [ ! -d "$TARGET_DIR" ]; then
echo "错误:目录不存在"
exit 1
fi
# 遍历所有.txt文件
for file in "$TARGET_DIR"/*.txt; do
if [ -f "$file" ]; then
# 使用attr命令添加扩展属性
attr -s description -V "$DESCRIPTION" "$file" 2>/dev/null
if [ $? -eq 0 ]; then
echo "已添加描述: $file"
else
echo "处理失败: $file"
fi
fi
done
echo "批量添加完成!"
Python 跨平台脚本(推荐)
# batch-add-description.py
import os
import sys
import platform
def add_description_to_files(directory, description, file_extensions=None):
"""批量添加文件描述信息"""
# 处理文件扩展名
if file_extensions:
if isinstance(file_extensions, str):
file_extensions = [file_extensions]
else:
file_extensions = ['.txt', '.pdf', '.doc', '.docx']
processed_count = 0
error_count = 0
# 遍历目录
for root, dirs, files in os.walk(directory):
for file in files:
# 检查文件扩展名
file_ext = os.path.splitext(file)[1].lower()
if file_ext in file_extensions:
file_path = os.path.join(root, file)
try:
if platform.system() == 'Windows':
# Windows系统使用comtypes(需要安装)
add_windows_metadata(file_path, description)
else:
# Linux/Mac使用扩展属性
add_extended_attributes(file_path, description)
print(f"✓ {file_path}")
processed_count += 1
except Exception as e:
print(f"✗ 错误 {file_path}: {str(e)}")
error_count += 1
print(f"\n处理完成!成功: {processed_count}, 失败: {error_count}")
def add_extended_attributes(file_path, description):
"""Linux/Mac添加扩展属性"""
if platform.system() == 'Linux':
import subprocess
# 使用attr命令
subprocess.run(['attr', '-s', 'user.description', '-V', description, file_path],
capture_output=True)
elif platform.system() == 'Darwin': # macOS
import xattr
xattr.setxattr(file_path, 'com.apple.metadata:kMDItemFinderComment',
description.encode('utf-8'))
def add_windows_metadata(file_path, description):
"""Windows添加文件属性"""
# 需要安装pywin32或使用PowerShell
import subprocess
script = f'''
$file = Get-Item "{file_path}"
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace($file.DirectoryName)
$item = $folder.ParseName($file.Name)
$item.ExtendedProperty("System.Comment") = "{description}"
'''
subprocess.run(['powershell', '-Command', script], capture_output=True)
if __name__ == "__main__":
# 使用示例
target_dir = input("请输入目标目录路径: ").strip()
description = input("请输入描述信息: ").strip()
extensions = input("请输入文件扩展名(用逗号分隔,默认: txt,pdf): ").strip()
if extensions:
extensions = [f".{ext.strip()}" for ext in extensions.split(',')]
else:
extensions = None
if os.path.exists(target_dir):
add_description_to_files(target_dir, description, extensions)
else:
print("错误:目录不存在!")
简单实用的BAT脚本(Windows)
@echo off
:: batch-add-description.bat
setlocal enabledelayedexpansion
:: 设置参数
set "DESCRIPTION=这是一个文件描述"
set "FILE_PATH=C:\Your\Directory"
:: 遍历文件
for %%f in ("%FILE_PATH%\*.txt") do (
echo 处理: %%f
:: 使用PowerShell添加描述
powershell -Command "
$file = Get-Item '%%f';
$shell = New-Object -ComObject Shell.Application;
$folder = $shell.Namespace($file.DirectoryName);
$item = $folder.ParseName($file.Name);
$item.ExtendedProperty('System.Comment') = '%DESCRIPTION%';
echo '已添加描述: ' + $file.Name
"
)
echo 处理完成!
pause
使用说明
-
Windows系统:
- 管理员权限运行PowerShell脚本
- 或使用兼容的BAT脚本
-
Linux系统:
chmod +x batch-add-description.sh ./batch-add-description.sh
-
Python脚本:
pip install xattr # macOS需要 pip install pywin32 # Windows需要 python batch-add-description.py
注意事项
- 不同文件系统支持程度不同(NTFS支持较好,FAT32不支持)
- 某些系统可能需要管理员/root权限
- 描述信息长度可能有限制
- 备份重要文件后再进行操作
需要我调整脚本以适应您的具体需求吗?