数据来源 $urldata 中的倒数第2列(子文件夹名称列)包含 /、\ 等特殊字符
某个文件夹重命名脚本
foreach ($i in 0..100) {
# 提取路径部分
$basePath = ($urldata[$i].split("`t")[-1]).trim()
$subPath = ($urldata[$i].split("`t")[-2].replace('"', '')).trim()
# 组合完整路径
$fullPath = "$basePath/$subPath"
# 替换字符以生成重命名路径
$renamePath = $subPath -replace '/', '__' -replace '\\', '_、' -replace ':', '_:'
$renamePath = "$basePath/$renamePath"
if ($fullPath -eq $renamePath) {continue}
if (Test-Path $fullPath) {
# 创建文件夹
New-Item -Path $renamePath -ItemType Directory -Force # 确保目录存在
# 移动文件
Move-Item "$fullPath/*" -Destination $renamePath -Force # 确保强制移动
# 删除原文件夹
Remove-Item -Path $fullPath -Recurse -Force # 确保递归删除
# 输出重命名路径以供检查
Write-Host "Rename Path: $renamePath, del Path: $fullPath"
} else {
$oldPath="Y:/$($renamePath.replace('__','_'))"
$newPath="Y:/$renamePath"
if(Test-Path $oldPath){ rename-item $oldPath $newPath }
Write-Host "Path does not exist: $fullPath, 修改文件夹 $oldPath 为 $newPath" # 如果路径不存在,输出提示
}
}
递归查找当前文件夹下的子文件夹,
Get-ChildItem -Recurse | Where-Object { $_.psiscontainer -and ($_.GetFileSystemInfos().Count -eq 0) } | ForEach-Object { Remove-Item $_;Write-Host "文件夹 $_ 已删除" }
PS, 对 PowerShell 中的 Get-ChildItem 帮助的理解感到有些疑惑。
`Get-ChildItem` doesn't display empty directories. When a `Get-ChildItem` command includes the Depth or
Recurse parameters, empty directories aren't included in the output.
延申阅读
标签:重命名,renamePath,replace,文件夹,Path,fullPath,PowerShell From: https://www.cnblogs.com/geyee/p/18542142