Reputation: 15891
I have the following script that loops through textfiles an replaces the letter a by b
$fileList = Get-ChildItem C:\Projekte\ps
foreach ($i in $fileList){ (Get-Content -Path $i.FullName ) -replace 'a' , 'b' | Set-Content -Path $i.FullName }
I works, and the result is written back to the original files. I need to write the content back to a new file. The name of the file is the original file but wit the extension ".new"
I expected something like
Set-Content -Path $i.FullName + '.new'
but thats obviously wrong.
Whats the right syntax for this problem?
Upvotes: 1
Views: 4590
Reputation: 126722
I suggest you )at list) filter text files so replace wont happen for other file extensions that may reside in that folder:
$fileList = Get-ChildItem D:\Scripts\Temp -Filter *.txt
foreach($i in $fileList)
{
$path = Join-Path -Path $i.DirectoryName -ChildPath ($i.BaseName + '.new')
(Get-Content -Path $i.FullName) -replace 'a','b' | Set-Content -Path $path
}
Upvotes: 2