3

I have a number of files with extension .psa in my Prevalidation folder and I want to:

  1. copy them one by one into my working folder
  2. rename the .psa file to psaload.csv
  3. run a set of commands against the file to load it to my db
  4. and then delete the csv file from my working folder.

This will be repeated for all the .psa files I have on my source folder.

So, the question is how do I execute the set of commands in a loop over as many .psa files as present.

Here's my piece of code testing for only one file in my Prevalidation folder -

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

<run the psaload.csv to load to my db>

This is what I intend to do -

Consider multiple .psa files in my C:\Downloads\Prevalidation folder.

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

I am looking for the syntax to run these set of commands for each files one by one as present in my /prevalidation folder.

3 Answers 3

5

Since all the other answers were quite horrible code and not very idiomatic PowerShell, here is my take (though untested):

# Get all .psa files
Get-ChildItem C:\Downloads\PreValidation\*.psa |
   ForEach-Object {
      # Load the file's contents, replace commas with spaces
      (Get-Content $_) -replace ',', ' ' |
         # and write it to the correct folder and file name
         Out-File C:\Downloads\WIP\psaload.csv

      # I guess you'd run whatever you're doing against the file here,
      # not after the loop

      Remove-Item C:\Downloads\WIP\psaload.csv
   }
Sign up to request clarification or add additional context in comments.

3 Comments

Two things to keep in mind with PowerShell: You have a pipeline and you're passing objects around. Both enable you to write better code that doesn't look as if Java came to a visit and put on Perl's shoes.
Joey, one small problem, all my commas are gone from the file. Thats because we are replacing all the commas with a space, right? My intention was to find for a \, inside a string and then replace it with a space instead of replacing all the commas with a blank space. This happens when some user put comma in the description field instead of a blank space. Could you please help me figuring out how to do it in your code? Thank You!!
So you were looking for the sequence backslash comma? Then you need to do -replace '\\,',' ' instead.
0

You can use foreach with Get-Item to do the loop. Get-Item will return a FileInfo object that you can use to get the file name (and other info) from. So you could do something like:

foreach($file in (Get-Item .\*.psa))
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name)";
}

etc.

4 Comments

MrKWatkins, thanks for your response. I didnt understand the part which says FullName? Is it part of the object returned from Fileinfo as you mentioned? Reason I am asking is that I want my psa files to be renamed to a particular file name which is psaload1.csv? Do I need to store it to a Newname object or something like that? Appreciate your time explaining this. I am very new to learning Powershell.
That's correct, FullName is part of the object returned. FullName is the full path + name + extension, (e.g. C:\Downloads\PreValidation\aaaa.psa) Name is the name + extension (e.g. aaaa.psa) and BaseName is just the name. (e.g. aaaa) Do you need to have different names for each file, e.g. psaload1, psaload2, etc? If so you could use a counter, i.e. set $i = 1 before the loop, then use "psaload$i.csv" for the filename, and do $i++ after to increase it.
Copy-Item $file C:\Downloads\Validation\WIP ... just sayin'.
Yeah, realised that last night... I think the reason I originally did the FileInfo stuff was that I misread the question and assumed he wanted aaaa.psa to be aaaa.csv. Should've deleted it really.
0

Try this:

$a = Get-Item .\*.psa

foreach($file in $a)
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name.replace(".psa",".psload.csv)";

remove-item $file

}

4 Comments

Christian, thanks for your post. So what about the rest of the commands? Do I follow them after the ; and end it with another ; inside the foreach loop?
Edit my answer adding remove-item for the old file
I think you should just use Move-Item in the first place, right?
@ChrisN Yes, you're right! but I've to change my answer because only now I've read about replacing "\," with ","!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.