title: Batch modifying directory with Powershell
body: <p>I need to update directory in many txt files</p>
  
  <pre><code>Input files:
  1.txt
  using c:\data\1.dta
  its own data
  
  2.txt
  using c:\data\2.dta
  its own data
  
  3.txt
  using c:\data\3.dta
  its own data
  
  Expected Output files:
  1.txt
  using C:\Data\Subfile\1.dta
  its own data
  
  2.txt
  using C:\Data\Subfile\2.dta
  its own data
  
  3.txt
  using C:\Data\Subfile\3.dta
  its own data
  </code></pre>
  
  <p>I've tried -replace but the results are strange: either all files have the same result or have all new directories (sell bellow)</p>
  
  <p>I want to update the oldpath into the newpath in all files. The code is as following:</p>
  
  <pre><code>$pathway='C:Data\Subfile\*.txt'
  $oldpath='c:\\data\\'
  $newpath='C:\Data\Subfile\'
  $content=Get-Content -path $pathway
  </code></pre>
  
  <p>Method 1:</p>
  
  <pre><code>$newline=((Get-Content -path $pathway -TotalCount 1) -replace $oldpath,$newpath)
  $content[0]= $newline
  
  This method will include all updated directories in every file:
  Wrong output:
  1.txt
  using C:\Data\Subfile\1.txt
  using C:\Data\Subfile\2.txt
  using C:\Data\Subfile\3.txt
  its own data
  
  2.txt
  using C:\Data\Subfile\1.txt
  using C:\Data\Subfile\2.txt
  using C:\Data\Subfile\3.txt
  its own data
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>$content[0]=$content[0]-replace $oldpath,$newpath
  
  This method will cause all file has the same new directory:
  Wrong output:
  1.txt
  using C:\Data\Subfile\1.txt 
  its own data
  
  2.txt
  using C:\Data\Subfile\1.txt 
  its own data
  
  3.txt
  using C:\Data\Subfile\1.txt 
  its own data
  
  $content | Set-Content -Path $pathway
  </code></pre>
  
  <p>Can someone help me with that? I want each file has its corresponding new directory. For 1.txt I want C:\Data\Subfile\1.txt, for 2.txt I want C:\Data\Subfile\2.txt etc.</p>
  
  <p>Thanks a lot!</p>