将%variablename%替换为htm文件中的%MyVariable%

时间:2017-02-03 20:56:55

标签: html powershell batch-file ms-word duplicates

如何编辑word文档?

SET /p MyNewPig="Enter My New Pig's Name: "
POWERSHELL(Get-Content c:\temp\test.htm) -replace '\%MyNewPig%\]', '%MyNewPig%' | Set-Content c:\temp\test.htm

1 个答案:

答案 0 :(得分:2)

就个人而言,我会创建一个小型PowerShell脚本,例如:

param ( [Parameter(Mandatory=$true)] [String] $file    = '',
        [Parameter(Mandatory=$true)] [String] $search  = '',
        [Parameter(Mandatory=$true)] [String] $replace = '' 
      )

[Int32] $Local:intRc   = 0;

try {
    (Get-Content -LiteralPath $file).replace( $search, $replace ) | Out-File -LiteralPath $file;
    } #try
catch [System.Exception] {
    Write-Host -Object ( 'ERROR : Exception was "{0}".' -f $_.Exception.Message );
    $intRc = -1;
    } #catch

exit $intRc;    

...然后调用它,例如:

 %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -file c:\xxxxxx\script.ps1 -file c:\yyyyy\test.txt -search "%OLD_NAME%" -replace "%NEW_NAME%"