Replace multiple strings in a file using powershell -


i using following coe replace string

$folders=get-childitem -path "c:\temp\database scripts" foreach($folder in $folders) {     write-host $folder     $spath=[string]::concat("c:\temp\database scripts\", $folder)     $subfolders=get-childitem $spath      foreach($subfolder in $subfolders )     {         if($subfolder -match "running scripts")         {             $subfolerpath=[string]::concat($spath,"\",$subfolder,"\*")               $files =get-childitem -path $subfolerpath -include "avevascripts*"             if($files -ne $null)             {                 foreach( $file in $files)                 {                 write-host $file;                 (get-content $file) | foreach-object {$_ -replace "database_user","fhghjgj" `                 -replace "database_password", "dfghfhjgjh"  } |set-content $file                  }             }         }            } } 

but ending following error.

set-content : input object cannot bound parameters command either because command not take pipeline input or input , properties not match of parameters take pipeline input.

please :)

remove $x in end of set-content. $x never declared.

also, simplify lot. ex:

get-childitem -filter "running scripts" -path "c:\temp\database scripts" -recurse | foreach-object {     get-childitem -path $_.fullname -filter "avevascripts*" -recurse | foreach-object {         (get-content $_.fullname) | foreach-object {             $_ -replace "database_user","fhghjgj" -replace "database_password", "dfghfhjgjh"         } | set-content $_.fullname     } } 

or find files includes "avevascripts" in it's name, check if full path includes "running scripts"

get-childitem -filter "avevascripts*" -path "c:\temp\database scripts" -recurse |  where-object { $_.fullname -like "*running scripts*" } |  foreach-object {     (get-content $_.fullname) | foreach-object {         $_ -replace "database_user","fhghjgj" -replace "database_password", "dfghfhjgjh"     } | set-content $_.fullname } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -