PowerShell重命名

编程入门 行业动态 更新时间:2024-10-28 07:33:57
PowerShell重命名 - 项目无法重命名(powershell Rename-Item fail to rename)

我的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.RenameItemCommand
 

I 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 $newname

Powershell不喜欢文件名中的方括号,更多内容在以下文章中 :

当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 $newname

otherwise use Move-Item

Move-Item -LiteralPath $folder.FullName $newname

Powershell 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.

更多推荐

本文发布于:2023-04-28 13:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!