2

Here is my for loop

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++
   }

}

I am getting following error

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
dur
  • 15,689
  • 25
  • 79
  • 125

1 Answers1

0

You should add -Force parameter to the Rename-Item to make it overwrite existing files.

If your code happens to generate multiple equal file names you could add a counter to the filename.

majkinetor
  • 8,730
  • 9
  • 54
  • 72