[英]Rename folders to lowercase in powershell
所以首先,是的,我在詢問之前確實進行了檢查,但沒有一個答案適用於我的特定情況。
我試過這個:
Get-ChildItem -Directory | %{
$NName = $_.Name.ToLowerInvariant()
$TempItem = Rename-Item -Path $_.FullName -NewName "x$NName" -PassThru
Rename-Item -Path $TempItem.FullName -NewName $NName
}
和這個:
#Get the directories / sub directories and rename to lowercase
Get-ChildItem -recurse |
?{ $_.PSIsContainer -And $_.Name -CMatch "[A-Z]" } |
%{
$NName = $_.Name.ToLowerInvariant()
# Set temporary name to enable rename to the same name; Windows is not case sensitive
$TempItem = Rename-Item -Path $_.FullName -NewName "x$NName" -PassThru
Rename-Item -Path $TempItem.FullName -NewName $NName
}
除了我自己嘗試過的其他東西之外,當目錄名稱很奇怪時,它們都不起作用:“ASddwAfaSd [sdfasd} []}] asdasd.asf”。
好的,所以我解決了這個問題,但我仍然會用答案來創建問題,以防萬一其他人必須處理這個問題: -LiteralPath
開關將忽略所有特殊字符,因此可以使用它來實現這個想要的效果:
Get-ChildItem -Directory | %{
$NewName = $_.Name.ToLowerInvariant()
$TempItem = Rename-Item -LiteralPath $_.FullName -NewName "a" -PassThru
Rename-Item -LiteralPath $TempItem.FullName -NewName $NewName
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.