admin管理员组文章数量:1316680
I'm trying to rename the following files in a batch:
image.part-Frag1
image.part-Frag10
image.part-Frag11
> Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
... but am getting the following error:
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\user\Des...html.part-Frag1:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\user\Des...tml.part-Frag10:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
...
I'm trying to rename the following files in a batch:
image.part-Frag1
image.part-Frag10
image.part-Frag11
> Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
... but am getting the following error:
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\user\Des...html.part-Frag1:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\user\Des...tml.part-Frag10:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot create a file when that file already exists.
At line:1 char:24
+ Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png"
...
Share
Improve this question
asked Jan 29 at 9:34
gargoylebidentgargoylebident
4371 gold badge3 silver badges15 bronze badges
1
|
3 Answers
Reset to default 3Although it seems these are not complete files (fragments of a larger download perhaps), the main issue with your code is that you need to use a scriptblock on the -NewName
parameter in order to use the $_
automatic variable as sirtao already showed in his answer.
But also, beware that piping files directly from Get-ChildItem into the Rename-Item cmdlet may result in trying to rename files that already have been renamed.
To prevent that from happening, enclose the Get-ChildItem part of the code in brackets so it will enumerate the files to fetch completely before sending them through the pipe to rename them.
(Get-ChildItem -Filter '*.part-frag*' -File) | Rename-Item -NewName { '{0}.png' -f $_.Name }
or first capture the enumerated files in a variable and use that to rename the files
$fragFiles = Get-ChildItem -Filter '*.part-frag*' -File
$fragFiles | Rename-Item -NewName { '{0}.png' -f $_.Name }
It does exist: using "$($_.Name).png"
you end up creating a file named .png
because $_
is not accessible the way you wrote it and thus defaults to null which gets converted to a empty string and then hained to .png
.
Solution: encapsulate it in a scriptblock: Get-ChildItem *frag* | Rename-Item -NewName { "$($_.Name).png" }
-whatif
is your friend:
echo hi > frag
Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png" -whatif
What if: Performing the operation "Rename File" on target "Item:
C:\Users\js\foo\frag Destination: C:\Users\js\foo\.png".
With scriptblock and slight changes. Usually if you can pipe to a parameter like -NewName
, you can use a scriptblock with it.
(Get-ChildItem *frag*) | Rename-Item -NewName { $_.Name + '.png' } -whatif
What if: Performing the operation "Rename File" on target "Item:
C:\Users\js\foo\frag Destination: C:\Users\js\foo\frag.png".
本文标签: powershellRenameItemCannot create a file when that file already existsit doesn39tStack Overflow
版权声明:本文标题:powershell - Rename-Item : Cannot create a file when that file already exists - it doesn't - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742006304a2412023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$_
variable (whose alias is$PSItem
) is only ever meaningfully defined inside script blocks ({ ... }
), such as inForEach-Object
andWhere-Object
calls, calculated properties, and delay-bind script blocks, which are required in your case. See this answer for details. – mklement0 Commented Jan 29 at 19:04