这是我的循环
function Generate_bin($input_1, $bin_file_name, $Codes){
$counter=1
foreach ($Code in $Codes)
{
Write-Host "Code = $Code"
$input_11 = "$input_1" + "$counter"
$pattern = "0x"
$New = ""
$Code = [regex]::replace($Code, $pattern, "$New")
$bin_file_name2 = "$bin_file_name"
$bin_file_name2 += "$Code" + ".bin"
#This utility generates "out.bin"
Invoke-Command -ScriptBlock {.\xyz.exe -i -t "$input_1"}
Rename-Item out.bin -NewName $bin_file_name2
$counter++
}
}
我收到以下错误
Rename-Item : Cannot create a file when that file already exists.
At C:\script\myParser_unique.ps1:18 char:7
+ Rename-Item out.bin -NewName $bin_file_name2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\v-ashi...Unified\out.bin:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
最佳答案
您应该将-Force参数添加到Rename-Item以使其覆盖现有文件。
如果您的代码碰巧生成了多个相等的文件名,则可以在文件名中添加一个计数器。
关于powershell - 当我想重命名powerShell中for循环中生成的文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923243/