ntr2def
asked on
powershell script to edit text
I've always been able to edit a script to replace something with something else for example
$Files=get-childitem . server.xml -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace 'directory="logs"', 'directory="E:/logfiles/To mcatLogs/A ccessLog"' } |
Set-Content $file.PSPath
}
Always works but today I came across replacing text that had special characters in such as:
$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = ${catalina.base}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}
When i run that it does nothing, edits nothing. Can anyone help?
$Files=get-childitem . server.xml -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace 'directory="logs"', 'directory="E:/logfiles/To
Set-Content $file.PSPath
}
Always works but today I came across replacing text that had special characters in such as:
$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = ${catalina.base}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}
When i run that it does nothing, edits nothing. Can anyone help?
ASKER
I got that but that's the original txt in the file which I'm trying to edit out.
ASKER
Didn't see the code snippet. Let me test
ASKER
it didnt work, no change in the document after i ran it
I can't full replicate what you're doing. But I've added a text file with the contents:
Line 1
String Begin 'directory = ${catalina.base}/logs' String End
Line 3
And run the following.
Line 1
String Begin 'directory = ${catalina.base}/logs' String End
Line 3
And run the following.
$files = ,(Get-ChildItem $env:temp | ? {$_.name -eq "test.txt"})
foreach ($file in $files)
{
$content = Get-Content $file.fullname
$content = $content | ForEach-Object {$_ -replace "'directory = \$\{catalina.base\}/logs'", "directory = E:/logfiles/TomcatLogs"}
Set-Content $file.fullname -Force -Value $content
}
This is working and putting out the expected output.
ASKER
it does work for a one liner i tried this approach with multiple instances for exmaple
Line 1
common.loader=${catalina.b ase}/lib,$ {catalina. base}/lib/ *.jar,${ca talina.hom e}/lib,${c atalina.ho me}/lib/*. jar
Line 2
line 3
Line 1
common.loader=${catalina.b
Line 2
line 3
$Files=get-childitem . catalina.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar", "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar,E:/Environment/jotmlib/*.jar"} |
Set-Content $file.PSPath
}
OK so in that case a whole lot has to be done differently. Are catalina.base and catalina.home completely different values? If they are you will require multiple replace statements.
ASKER
actually I'm trying to add:
,E:/Environment/jotmlib/*. jar
to the end of:
common.loader=${catalina.b ase}/lib,$ {catalina. base}/lib/ *.jar,${ca talina.hom e}/lib,${c atalina.ho me}/lib/*. jar
,E:/Environment/jotmlib/*.
to the end of:
common.loader=${catalina.b
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked perfectly and i appreciate your explanation thank you for this
Open in new window