Combine file IO for -replace and Set-Content in PowerShell -
i have following script:
$allfiles = get-childitem "./" -recurse | { ($_.extension -eq ".ts")} foreach($file in $allfiles) { # find , replace dash cased contents of files (get-content $file.pspath) | foreach-object {$_ -replace "my-project-name", '$appnamedashcased$'} | set-content $file.pspath # find , replace dash cased contents of files (get-content $file.pspath) | foreach-object {$_ -replace "myprojectname", '$appnamecamelcased$'} | set-content $file.pspath # find , replace dash cased contents of files (get-content $file.pspath) | foreach-object {$_ -replace "myprojectname", '$appnamepascalcased$'} | set-content $file.pspath }
it takes file , replacing, saves file. takes same file , more replacing saves file again. 1 more time.
this works, seems inefficient.
is there way replacing , save file once?
(if possible, prefer keep readable style of powershell.)
sure, chain replaces inside foreach-object
block:
$allfiles = get-childitem "./" -recurse | { ($_.extension -eq ".ts")} foreach($file in $allfiles) { (get-content $file.pspath) | foreach-object { # find , replace dash cased contents of files $_ -replace "my-project-name", '$appnamedashcased$' ` -replace "myprojectname", '$appnamecamelcased$' ` -replace "myprojectname", '$appnamepascalcased$' } | set-content $file.pspath }
Comments
Post a Comment