title: Powershell script to copy and rename files in a loop
body: <p>I have a number of files with extension .psa in my Prevalidation folder and I want to:</p>
  
  <ol>
  <li>copy them one by one into my working folder</li>
  <li>rename the .psa file to psaload.csv</li>
  <li>run a set of commands against the file to load it to my db</li>
  <li>and then delete the csv file from my working folder. </li>
  </ol>
  
  <p>This will be repeated for all the .psa files I have on my source folder. </p>
  
  <p>So, the question is how do I execute the set of commands in a loop over as many .psa files as present.</p>
  
  <p>Here's my piece of code testing for only one file in my Prevalidation folder -</p>
  
  <pre><code>Copy-Item  C:\Downloads\PreValidation\*.psa C:\Downloads\Validation\WIP
  Rename-Item 'C:\Downloads\Validation\WIP\abc 1234.psa' 'psaload1.csv'
  
  Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
  
  Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
  
  &lt;run the psaload.csv to load to my db&gt;
  </code></pre>
  
  <p>This is what I intend to do -</p>
  
  <p>Consider multiple .psa files in my C:\Downloads\Prevalidation folder.</p>
  
  <pre><code>For each C:\Downloads\PreValidation\*.psa
  
  BEGIN LOOP
  
  Copy-Item  C:\Downloads\PreValidation\aaaa.psa C:\Downloads\Validation\WIP\aaaa.psa
  Rename-Item 'C:\Downloads\Validation\WIP\aaaa.psa' 'psaload1.csv'
  
  Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
  
  Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
  
  
  END LOOP
  </code></pre>
  
  <p>I am looking for the syntax to run these set of commands for each files one by one as present in my /prevalidation folder.</p>