#!/bin/bash

# 检查是否提供了目录参数
if [ $# -ne 1 ]; then
    echo "用法: $0 <目标目录>"
    exit 1
fi

target_dir="$1"

# 检查目录是否存在
if [ ! -d "$target_dir" ]; then
    echo "错误: 目录 $target_dir 不存在!"
    exit 1
fi

# 进入目标目录
cd "$target_dir" || exit 1

# 遍历当前目录下的所有文件（不包括子目录）
for file in *; do
    # 跳过目录，只处理文件
    if [ -f "$file" ]; then
        # 处理文件名：
        # 1. 将空格替换为下划线
        # 2. 移除 [ ] ( ) + , & 这些字符
        new_name=$(echo "$file" | tr ' ' '_' | tr -d '[]()+,&' )
        
        # 如果新文件名与原文件名不同，则进行重命名
        if [ "$new_name" != "$file" ]; then
            mv -v "$file" "$new_name"
        fi
    fi
done

echo "文件名处理完成！"