2

这是我的 for 循环

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
4

1 回答 1

0

您应该将 -Force 参数添加到 Rename-Item 以使其覆盖现有文件。

如果您的代码恰好生成多个相同的文件名,您可以在文件名中添加一个计数器。

于 2016-04-28T18:57:59.283 回答