0

How do I write the filename (basename and not the fullname) into the same file by replacing a string present in the file. Basically, I have a bunch of .psf files inside a folder called C:\Downloads\PreValidation.

  1. I want to change each file to a .csv format and
  2. while doing so I want to replace the string called 'Space Planning Project' present inside one of the records of the file with the filename without the extension.

This is the script that I am trying to run -

foreach($file in (dir C:\Downloads\PreValidation\*.psf)){
$fromdir = "C:\Downloads\Prevalidation\*.psf"
$fullname = gi $fromdir| select fullname
$b = $fullname[0]
$b.fullname
Copy-Item $file.fullname C:\Downloads\Validation\WIP\psaload1.csv
Get-Content C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv 
}

but its erroring out saying

 Get-Content : Cannot find path 'C:\Downloads\Validation\WIP\psaload.csv' because it  does not exist.
 At C:\cap_sps\powershell\testfilename.ps1:7 char:12
 + Get-Content <<<<  C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object  {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv
+ CategoryInfo          : ObjectNotFound: (C:\Downloads\Validation\WIP\psaload.csv:String) [Get-Content], ItemNotF
oundException
 + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Note that I am using my final psaload.csv to load into an Oracle database and in the last step of my script I am removing this file so that I can run the same commands in loop for the rest of the .psf files present in the above folder.

Any help is deeply appreciated.

Thanks, Sanders.

2 Answers 2

4

There is a lot of redundancy in your code, with the dir in the loop and then using the file to get the item again. Also you are copying to the same csv file for each of the psf files and the copy line mentions a file psaload1.csv, but you are trying to get the content of psaload.csv, causing the error that you see.

Try something like this ( I am making some assumptions here and untested):

gci C:\Downloads\PreValidation\*.psf | %{
$file = $_
gc $file.fullname | % {$_ -replace "Space Planning Project",$file.BaseName } | Set-Content "C:\Downloads\Validation\WIP\$($file.BaseName).csv"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Manoj - this is working like a charm. Since my sqlloader is expecting to load a file with name psaload.csv, I just changed the filename in the Set-Content as psaload.csv. here is my code based on yours - gci C:\Downloads\PreValidation*.psf | %{ $file = $_ gc $file.fullname | % {$_ -replace "Space Planning Project",$file.BaseName } | Set-Content "C:\Downloads\Validation\WIP\psaload.csv" cmd.exe /k C:\CAP_SPS\Batchscripts\runfixloader.bat Remove-item C:\Downloads\Validation\WIP\psaload.csv }
0

To get the BaseName of a file object , use the BaseName property. You can modify your script like so:

$files = gi $fromdir| select fullname,basename
$fullname = $files[0].fullname
$b = $files[0].basename

As for the error message, you are trying to retrieve the contents of a file that does not exist. Your copy line

Copy-Item $file.fullname C:\Downloads\Validation\WIP\psaload1.csv

has a "1" in the file name, but in the next line, your filename to Get-Content does not.

4 Comments

Thanks for your response zdan. But I am getting the following error now - Copy-Item : Cannot bind argument to parameter 'Path' because it is null. At C:\cap_sps\powershell\testfilename.ps1:7 char:10 + Copy-Item <<<< $files.$fullname C:\Downloads\Validation\WIP\psaload1.csv + CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand Get-Content : Cannot find path 'C:\Downloads\Validation\WIP\psaload1.csv' because it does not exist.
Contd. from previous comment - + Get-Content <<<< C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object {$_.replace("Space Planning Project","$b" )} | Set-Content C:\Downloads\Validation\WIP\psaload.csv + CategoryInfo : ObjectNotFound: (C:\Downloads\Validation\WIP\psaload1.csv:String) [Get-Content], ItemNot FoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand. Adding my code in the below comment -
foreach($file in (dir C:\Downloads\PreValidation\*.psf)){ $fromdir = "C:\Downloads\Prevalidation\*.psf" $files = gi $fromdir| select fullname,basename $fullname = $files[0].fullname $b = $files[0].basename Copy-Item $files.$fullname C:\Downloads\Validation\WIP\psaload1.csv Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object {$_.replace("Space Planning Project","$b")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv cmd.exe /k C:\CAP_SPS\Batchscripts\runfixloader.bat Remove-item C:\Downloads\Validation\WIP\psaload.csv Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
@user982201: try manojlds answer, it should do what you're looking for.

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.