包含特殊符号的文件夹,在其父目录层面不能直接删除
Windows 版本
# 获取当前目录下的所有目录
$directories = Get-ChildItem -Path "." -Directory
# 遍历所有目录
foreach ($dir in $directories) {
# 检查目录名是否包含 "!"、"?"、"," 或空格
if ($dir.Name -like "*!*") -or ($dir.Name -like "*?*") -or ($dir.Name -like "*,*") -or ($dir.Name -like "* *") {
# 输出警告信息
Write-Host "About to delete folder: $($dir.FullName)"
# 等待用户输入
$userInput = Read-Host "Press 'y' to confirm deletion, any other key to skip"
# 如果用户输入 "y",则删除该目录
if ($userInput -eq 'y') {
Remove-Item -Recurse -Force $dir.FullName
}
}
}
Linux 版本
#!/bin/bash
current_dir=$(pwd)
directories=$(find $current_dir -type d -name "*[!?, ]*")
for directory in $directories; do
if [[ $directory =~ "!" || $directory =~ "?" || $directory =~ "," || $directory =~ " " ]]; then
echo "The directory $directory contains special characters. Do you want to delete it?"
read answer
if [[ $answer == "y" || $answer == "Y" ]]; then
rm -rf $directory
fi
fi
done
标签:Name,删除,directories,answer,文件夹,directory,特殊符号,dir,like
From: https://blog.51cto.com/first01/7512266