Sunday, 15 April 2012

PowerShell get-child item and get-content a few folders deep -



PowerShell get-child item and get-content a few folders deep -

my script below works 1 folder using "$_" before prior location of file:

get-childitem e:\websystems\configs\ | foreach-object {get-content e:\websystems\configs\$_\web.config} | foreach-object {$_ -replace "web1", "web2"} | set-content e:\websystems\configs\$_\web.config}

how 2 folders deep? ex: e:\websystems\configs\folder1\folder2\web.config

the next script doesn't work.

get-childitem e:\websystems\configs\ | foreach-object {get-content e:\websystems\configs\$_\$_\web.config} | foreach-object {$_ -replace "web1", "web2"} | set-content e:\websystems\configs\$_\$_\web.config}

this should work:

get-childitem -recurse -include web.config | foreach-object { $name = $_.fullname; get-content $name } | foreach-object {$_ -replace "web1", "web2" } | set-content $name

i implore test on isolated temporary directory. might want seek @ first:

get-childitem -recurse -include web.config | foreach-object { $name = $_.fullname; get-content $name } | foreach-object {$_ -replace "web1", "web2" } | set-content "$name.modified"

powershell

No comments:

Post a Comment