最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Rename-Item : Cannot create a file when that file already exists - it doesn't - Stack Overflow

programmeradmin1浏览0评论

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
  • In short: The automatic $_ variable (whose alias is $PSItem) is only ever meaningfully defined inside script blocks ({ ... }), such as in ForEach-Object and Where-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
Add a comment  | 

3 Answers 3

Reset to default 3

Although 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".
发布评论

评论列表(0)

  1. 暂无评论