title: Replacing multiple texts not getting saved
body: <p>I've a bunch of files in which I need to replace content like for e.g. wherever there is 'AA' I need to replace with 'E1', 'A1' with 'P4'. The same content needs to be changed differently in different files. So for example in the 2nd file 'AA' would become 'P1', 'A1' would become 'E1', etc. To accomplish this I've an Excel sheet with 2 columns like the below:</p>
  
  <pre>
  TC     CodeChange
  086    AA-E1; A1-P2
  099    AA-P2; A1-E1; A2-E2; Z3-E3
  100    AA-P2; A1-E2; A2-E3; Z3-O3
  </pre>
  
  <p>PowerShell script which I wrote for the above:</p>
  
  <p>Script 1:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>function func3 {
      Param($arr3, $pat)
      $arr3.GetEnumerator() | ?{$_.key -like $pat} | ForEach-Object {
          $output = $_.value
          return $output
      }
  }
  
  $src  = "C:\...xlsx"
  $src1 = "C:\...\..."
  
  $sheetName = "Sheet1"
  
  $arr = @{};
  
  $objExcel = New-Object -ComObject Excel.Application
  $workbook = $objExcel.Workbooks.Open($src)
  $sheet = $workbook.Worksheets.Item($sheetName)
  $objExcel.Visible = $false
  
  $rowMax = ($sheet.UsedRange.Rows).count
  
  $rowTC, $colTC = 1, 1
  $rowCodeChange, $colCodeChange = 1, 2
  
  for ($i=1; $i -le $rowMax-1; $i++) {
      $TC = $sheet.Cells.Item($rowTC+$i, $colTC).Text
      $CodeChg = [String]($sheet.Cells.Item($rowCodeChange+$i, $colCodeChange).Text)
  
      if ($arr.ContainsKey($TC) -eq $false) {
          $arr.Add($TC, $CodeChg)
      }
  }
  
  $inputfiles = (Get-ChildItem -Path $src1 -Recurse)
  
  foreach ($inputfile in $inputfiles) {
      $pat1 = $inputfile.Name.SubString(8, 3)
      $val  = func3 $arr $pat1
  
      $arry1 = $val -split ';'
      Write-Host $arry1.Length
  
      $j = 0
      do {
          #skipping these 3 items from getting replaced
          if (($arry1[$j].Trim() -ne "S1") -and ($arry1[$j].Trim() -ne "S2") -and ($arry1[$j].Trim() -ne "S3")) {
              (Get-Content $inputfile.FullName) | ForEach-Object {
                  $_ -replace "$($arry1[$j].Split('-')[0])","$($arry1[$j].Split('-')[1])"
              } | Set-Content $inputfile.FullName
          }
          $j++
      } while ($j -le ($arry1.Length-1))
  }
  
  $objExcel.Quit()
  </code></pre>
  
  <p>Script 2:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>function func3 {
      param($arr3, $pat)
      $arr3.GetEnumerator() | ?{$_.key -like $pat} | ForEach-Object {
          $output=$_.value
          return $output
      }
  }
  
  $src  = "C:\...xlsx"
  $src1 = "C:\..."
  
  $sheetName = "Sheet1"
  
  $arr = @{};
  
  $objExcel = New-Object -ComObject Excel.Application
  $workbook = $objExcel.Workbooks.Open($src)
  $sheet = $workbook.Worksheets.Item($sheetName)
  $objExcel.Visible = $false
  
  $rowMax = ($sheet.UsedRange.Rows).Count
  
  $rowTC, $colTC = 1, 1
  $rowCodeChange, $colCodeChange = 1, 2
  
  for ($i=1; $i -le $rowMax-1; $i++) {
      $TC = $sheet.Cells.Item($rowTC+$i, $colTC).Text
      $CodeChg = [String]($sheet.Cells.Item($rowCodeChange+$i, $colCodeChange).Text)
  
      if ($arr.ContainsKey($TC) -eq $false) {
          $arr.Add($TC, $CodeChg)
      }
  }
  
  $inputfiles = (Get-ChildItem -Path $src1 -Recurse)
  
  foreach ($inputfile in $inputfiles) {
      $pat1 = $inputfile.Name.SubString(8, 3)
      $val  = func3 $arr $pat1
  
      $arry1 = $val -split ';'
      Write-Host $arry1.Length
  
      $j = 0
      do {
          #skipping these 3 items from getting replaced
          if (($arry1[$j].Trim() -ne "S1") -and ($arry1[$j].Trim() -ne "S2") -and ($arry1[$j].Trim() -ne "S3")){
  
              $content = [System.IO.File]::ReadAllText($inputfile.FullName).Replace($arry1[$j].Split('-')[0], $arry1[$j].Split('-')[1])
              [System.IO.File]::WriteAllText($inputfile.FullName, $content)
              Write-Host $arry1[$j].Split('-')[0]' replaced with '$arry1[$j].Split('-')[1]' in file: '$inputfile.FullName
          }
          $j++
      } while ($j -le ($arry1.Length-1))
  }
  
  $objExcel.Quit()
  </code></pre>
  
  <p>The folder where the files are has the files having names containing the same digits in the 'TC' column in my Excel sheet. Example:</p>
  
  <pre>
  TC 086.txt
  TC 099.txt
  etc.
  </pre>
  
  <p>That way after I import the contents of the Excel into a hashtable I extract the digits from the filenames and get the corresponding values for the same key in the hashtable. For example the value for the key '086' from the hashtable would be 'AA-E1; A1-P2'. Then I split the items to be replaced from the hashtable value (separated by <code>;</code>) and then store that in an array. The using a loop I try to replace the contents of each file based on the data retrieved from the spreadsheet.</p>
  
  <p>The issue I'm facing with both the approaches is that only the 1st item in each file is getting replaced. The rest of the items are not getting replaced. For example only 'AA' value in file 'TC 086.txt' is getting replaced with 'E1'. 'A1' is not getting replaced with 'P2'.</p>