Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:22:04+00:00 2026-05-28T20:22:04+00:00

How do I write the filename (basename and not the fullname) into the same

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:22:05+00:00Added an answer on May 28, 2026 at 8:22 pm

    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"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a unicode string (s) which I want to write into a file.
I write the code below to delete a file: FileInfo file = new FileInfo(filename);
Can I specify what file I want to write into in C++? I want
I have the following code. QString fileName = QFileDialog::getSaveFileName( this, tr(Output Image file), (),
I have implemented a file selector with a combobox. I want to write the
Suppose I have a process call A which call fopen(filename,w); every second, and write
I have Winforms application that must create write to certain configuration file during usage.
i trying to save this string with a file list into a file but
I want to write an app that takes in a model filename via cmd
I'm trying to write a regex that will parse out the directory and filename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.