replace string if you dont' know rest of string in PowerShell -


please help. trying figure out how replace string in powershell, don't know rest of string. have this:

(get-content $file) -replace[regex]::escape('file='*''),('file='+$_.basename) | set-content $file 

i don't know comes after file=

i tried code, replaces multiple times instead of once.

so trying replace file=* filename=$_.basename.

thanks looking.

just fyi using latest version of powershell community extensions (http://pscx.codeplex.com), there new command called edit-file handles sort of thing nicely (works hard preserve file's original encoding):

get-item test.txt | foreach {$bn=$_.basename; $_} |      edit-file -pattern '(file=).*' -replace "`${1}$bn"  

in theory shouldn't need foreach stage seems i've found limitation in how -pipelinevariable not work parameters aren't pipeline bound. hmm, add pscx backlog.


Comments