我的powershell脚本:
$dst = 'C:\Temp' #Get all folders in $dst $folders = Get-ChildItem $dst | ?{ $_.PSIsContainer } foreach($folder in $folders) { $cnt = (Get-ChildItem -filter *.txt $folder | Measure-Object).Count $base = ($folder.FullName -split " \[.*\]$")[0] $newname = $("{0} [{1}]" -f $base,$cnt) Write-Host $folder.FullName "->" $newname Rename-Item $folder.FullName $newname }问题
在我第一次运行时,我得到了这个:
PS C:\Temp> C:\Temp\RenameFolders.ps1 C:\Temp\m1 -> C:\Temp\m1 [1]在我的第二次运行中,我得到这个:
PS C:\Temp> C:\Temp\RenameFolders.ps1 C:\Temp\m1 [1] -> C:\Temp\m1 [0] Rename-Item : Cannot rename because item at 'C:\Temp\m1 [1]' does not exist. At C:\Temp\RenameFolders.ps1:15 char:5 + Rename-Item $folder.FullName $newname + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand我知道问题出在'['和']',但我真的不明白为什么。
有人能解释我为什么是这个问题?
My powershell script:
$dst = 'C:\Temp' #Get all folders in $dst $folders = Get-ChildItem $dst | ?{ $_.PSIsContainer } foreach($folder in $folders) { $cnt = (Get-ChildItem -filter *.txt $folder | Measure-Object).Count $base = ($folder.FullName -split " \[.*\]$")[0] $newname = $("{0} [{1}]" -f $base,$cnt) Write-Host $folder.FullName "->" $newname Rename-Item $folder.FullName $newname }The problem
On my first run I get this:
PS C:\Temp> C:\Temp\RenameFolders.ps1 C:\Temp\m1 -> C:\Temp\m1 [1]On my second run I get this:
PS C:\Temp> C:\Temp\RenameFolders.ps1 C:\Temp\m1 [1] -> C:\Temp\m1 [0] Rename-Item : Cannot rename because item at 'C:\Temp\m1 [1]' does not exist. At C:\Temp\RenameFolders.ps1:15 char:5 + Rename-Item $folder.FullName $newname + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommandI know that the problem is in '[' and ']', but I realy can't understand why.
Can someone explain me why is that a problem?
最满意答案
如果您正在运行PS 3 +,请将-LiteralPath切换至您的重命名:
Rename-Item -LiteralPath $folder.FullName $newname否则使用Move-Item
Move-Item -LiteralPath $folder.FullName $newnamePowershell不喜欢文件名中的方括号,更多内容在以下文章中 :
当V2将方括号添加到通配符集以支持“blobbing”时,这成为V2的一个问题。
从get-help about_wildcards :
Windows PowerShell支持以下通配符。
通配符描述示例匹配不匹配
匹配零或a * a,ag,苹果香蕉多字符
? 完全匹配?在某个位置上运行一个字符
[]匹配范围[al] ook书,厨师,看看人物
[]匹配指定的[bc] ook书,厨师勾人物
[和]是特殊字符。
If you are running PS 3+ add -LiteralPath switch to your rename:
Rename-Item -LiteralPath $folder.FullName $newnameotherwise use Move-Item
Move-Item -LiteralPath $folder.FullName $newnamePowershell doesn't like square brackets in filenames, more in the following post:
This became an issue with V2 when they added the square brackets to the wildcard character set to support "blobbing".
From get-help about_wildcards:
Windows PowerShell supports the following wildcard characters.
Wildcard Description Example Match No match
Matches zero or a* A, ag, Apple banana more characters
? Matches exactly ?n an, in, on ran one character in the specified position
[ ] Matches a range [a-l]ook book, cook, look took of characters
[ ] Matches specified [bc]ook book, cook hook characters
[ and ] are special characters.
更多推荐
发布评论