Reputation: 9950
I have a folder with multiple files. I want to read each file and replace with whatever the user passes to the application (a parameter). After the content of the file is replaced, I want to save the file in a new folder.
This is what I have so far:
gci C:\Users\Person\Documents\Test1\*.bat -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace "<client_instance>", "ClientName"}) | Set-Content $_
}
My first question is, is this correct and secondly, how do I change "Set-Content" so that it saves the files in a different folder?
I want to use variables for the source and destination:
$sourePath
$destinationPath
I'm new to PowerShell so any help would be greatly appreciated. Thanks.
Upvotes: 0
Views: 93
Reputation: 1010
Yes, your code is fine. You can build your new path like this:
$files = gci C:\Users\Person\Documents\Test1\*.bat -recurse
$destinationpath = "C:\users\person\documents\test2"
ForEach ($file in $files){
Get-Content $file.fullname | ForEach {$_ -replace "<client_instance>", "ClientName"} | set-content "$destinationpath\$($file.basename)"
}
You might have to tweak it here and there to fit your needs.
Upvotes: 1