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.
- I want to change each file to a .csv format and
- 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.