本文目录导读:

Windows PowerShell 脚本
# 文件名非法字符去除脚本 (Windows)
# 保存为 Remove-BadChars.ps1 并右键"使用 PowerShell 运行"
# 设置目标文件夹路径
$targetFolder = "C:\Your\Path\Here" # 修改为实际路径
# 如果不指定路径,则使用当前目录
if (-not (Test-Path $targetFolder)) {
$targetFolder = Get-Location
}
Write-Host "正在处理文件夹: $targetFolder"
Write-Host ""
# Windows 文件名非法字符
$invalidChars = [System.IO.Path]::GetInvalidFileNameChars()
# 获取所有文件
$files = Get-ChildItem -Path $targetFolder -File
$renamedCount = 0
$errorCount = 0
foreach ($file in $files) {
$newName = $file.Name
# 检查是否包含非法字符
$hasInvalidChars = $false
foreach ($char in $invalidChars) {
if ($newName.Contains($char)) {
# 用空格替换非法字符
$newName = $newName.Replace($char, ' ')
$hasInvalidChars = $true
}
}
# 去除多余空格(可选)
$newName = ($newName -replace '\s+', ' ').Trim()
# 如果文件名发生了改变,则重命名
if ($hasInvalidChars -and $newName -ne $file.Name) {
$newPath = Join-Path $file.DirectoryName $newName
# 处理重名文件
$counter = 1
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($newName)
$extension = [System.IO.Path]::GetExtension($newName)
while (Test-Path $newPath) {
$newName = "$baseName($counter)$extension"
$newPath = Join-Path $file.DirectoryName $newName
$counter++
}
try {
Rename-Item -Path $file.FullName -NewName $newName -ErrorAction Stop
Write-Host "重命名: '$($file.Name)' -> '$newName'" -ForegroundColor Green
$renamedCount++
}
catch {
Write-Host "错误: 无法重命名 '$($file.Name)' - $_" -ForegroundColor Red
$errorCount++
}
}
}
Write-Host ""
Write-Host "处理完成! 重命名 $renamedCount 个文件, 失败 $errorCount 个."
更简单的 PowerShell 单行命令
# 直接在 PowerShell 中运行(无需脚本文件)
# 替换路径中的非法字符为空格
$folder = "C:\Your\Path"
Get-ChildItem $folder -File | ForEach-Object {
$newName = [regex]::Replace($_.Name, '[<>:"/\\|?*]', ' ')
$newName = ($newName -replace '\s+', ' ').Trim()
if ($newName -ne $_.Name) {
Rename-Item $_.FullName $newName
}
}
Linux/macOS Bash 脚本
#!/bin/bash
# 文件名非法字符去除脚本 (Linux/macOS)
# 保存为 remove_bad_chars.sh 并运行: chmod +x remove_bad_chars.sh && ./remove_bad_chars.sh
# 设置目标文件夹(默认为当前目录)
TARGET_DIR="${1:-.}"
echo "正在处理文件夹: $TARGET_DIR"
echo ""
# 计数器
renamed=0
errors=0
# 遍历所有文件
while IFS= read -r -d '' file; do
dir=$(dirname "$file")
old_name=$(basename "$file")
# 替换 Linux/macOS 文件系统中的非法字符(主要是 / 和 \0)
# 注意:Linux 只禁止 "/" 和 null 字符,但为了兼容性,也替换 Windows 常见的非法字符
new_name=$(echo "$old_name" | sed 's/[<>:"\/\\|?*]//g' | sed 's/ */ /g; s/^ *//; s/ *$//')
# 如果替换后文件名为空,设置一个默认名称
if [ -z "$new_name" ]; then
new_name="unnamed_$(date +%s)"
fi
# 如果文件名发生了变化,则重命名
if [ "$new_name" != "$old_name" ]; then
new_path="$dir/$new_name"
# 处理重名文件
counter=1
base_name="${new_name%.*}"
extension="${new_name##*.}"
if [ "$base_name" = "$extension" ]; then
extension=""
else
extension=".$extension"
fi
while [ -e "$new_path" ]; do
new_name="${base_name}(${counter})${extension}"
new_path="$dir/$new_name"
counter=$((counter + 1))
done
if mv "$file" "$new_path" 2>/dev/null; then
echo "重命名: '$old_name' -> '$new_name'"
renamed=$((renamed + 1))
else
echo "错误: 无法重命名 '$old_name'" >&2
errors=$((errors + 1))
fi
fi
done < <(find "$TARGET_DIR" -type f -print0)
echo ""
echo "处理完成! 重命名 $renamed 个文件, 失败 $errors 个."
使用说明
Windows 用户
- 打开记事本,粘贴 PowerShell 脚本
- 修改
$targetFolder变量为目标文件夹路径 - 保存为
Remove-BadChars.ps1 - 右键点击文件 → "使用 PowerShell 运行"
更安全的方法(推荐)
使用上面的单行命令,直接在 PowerShell 中执行:
$folder = "C:\你的文件夹路径"
Get-ChildItem $folder -File | ForEach-Object {
$new = [regex]::Replace($_.Name, '[<>:"/\\|?*]', ' ')
$new = ($new -replace '\s+', ' ').Trim()
if ($new -ne $_.Name) { Rename-Item $_.FullName $new }
}
被替换的非法字符
Windows 替换的字符: < > : " / \ | ? *
Linux 替换的字符: (以及可选的其他字符)
脚本会将非法字符替换为空格,并自动处理重名文件。