在查找和替换脚本后运行bat命令

时间:2016-08-29 10:32:19

标签: windows batch-file command-line

我正在使用以下脚本来查找和替换XML中的文本

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular-animate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular-aria.js"></script>
<script src="//rawgit.com/angular/bower-material/master/angular-material.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">

<div ng-app="myApp">
<div ng-controller="main">
  <label>search</label>
  <input ng-model="searchString"/>
  <div ng-if="!searchString">
<module>
    <ti-tle>User Management</ti-tle>
    <br>
    <tag-group>
        <tag>User Management</tag>
    </tag-group>
    <info-group>
        <info><md-icon class="material-icons ltr">perm_identity</md-icon>published by: Ha ba</info>
        <info><md-icon class="material-icons ltr">folder</md-icon>User Management</info>
        <info><md-icon class="material-icons ltr">publish</md-icon>published: 25 May 2016</info>
    </info-group>
    <hr>

    In <bold>AMe</bold>, you can manage multiple bank accounts
    <br><br>
    <sub-title>
        Introduction Accounting
    </sub-title>
    The Sales Planner is a useful step-by-step guide created to help you implement your sales funnel
    <br>
  Accounting
    Go to <bold>
  Accounting</bold> ‣ <bold>Configuration</bold> ‣ <bold>Bank Accounts</bold> and click on the Bank item. Edit it
    <note><md-icon class="material-icons">error_outline</md-icon>
       will detect the bank account type (e.g. IBAN) to allow some payment method like SEPA.
    </note>
    <br><br>
    <sub-title>
        Set up your first sales team
    </sub-title>
    For example, if within your company Tim is selling products and John is selling maintenance contracts, they will be assigned to different teams and will only receive opportunities that make sense to them.


    <br><br>
    <sub-title>
        Set up incoming email to generate opportunities
    </sub-title>
    In  CRM, one way to generate opportunities into your sales team is to create a generic email address as a trigger. For example, if
</module>

  
  </div>
  <div ng-if="searchString"  ng-bind-html="content | highlight:searchString"></div>

</div>
</div>

我也有一些命令在这部分后运行,但批处理文件在执行上述部分后退出。

你可以建议我在上述部分后继续使用脚本吗?

1 个答案:

答案 0 :(得分:2)

批处理代码显然是从其他来源一起复制而没有真正理解代码有很多小错误。下面的固定代码适用于名为new-text的文件。

@echo off
setlocal

call :FindReplace "#TargetfromODconfig#" "oldtest" new-text

rem INSERT HERE YOUR OTHER CODE.

endlocal
exit /B

rem The command above exits processing of this batch file to
rem avoid an unwanted fall through to the subroutine below.


rem Subroutine FindReplace searches for all occurrences of the string passed
rem as first parameter to this subroutine in all files matching the file name
rem or file name pattern passed as third parameter in current directory or
rem any subdirectory and replaces all found strings in all found files with
rem the string passed as second parameter.

:FindReplace <findstr> <replstr> <file>
set "TempXmlFile=%TEMP%\tmp.xml"
if not exist "%TEMP%\_.vbs" call :MakeReplace

for /F "tokens=*" %%a in ('dir "%~3" /A-D /B /ON /S 2^>nul') do (
    for /F "usebackq" %%b in (`%SystemRoot%\System32\Findstr.exe /I /M /C:"%~1" "%%a"`) do (
        echo(&Echo Replacing "%~1" with "%~2" in file "%%~nxa"
        <"%%a" %SystemRoot%\System32\cscript.exe //nologo "%TEMP%\_.vbs" "%~1" "%~2">"%TempXmlFile%"
        if exist "%TempXmlFile%" move /Y "%TempXmlFile%" "%%~fa" >nul
    )
)
del "%TEMP%\_.vbs"
goto :EOF

rem The command above exits subroutine FindReplace and batch processing
rem is continued on the line below the line which called this subroutine.


rem Subroutine MakeReplace just creates a small Windows console script.

:MakeReplace
>"%TEMP%\_.vbs" echo with Wscript
>>"%TEMP%\_.vbs" echo set args=.arguments
>>"%TEMP%\_.vbs" echo .StdOut.Write _
>>"%TEMP%\_.vbs" echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>"%TEMP%\_.vbs" echo end with
goto :EOF

rem The command above exits subroutine MakeReplace and batch processing
rem is continued on the line below the line which called this subroutine.

new-text是文件或文件名模式的奇怪名称。我想代码应该在* .xml文件上运行,因此应该使用此XML文件的名称而不是new-text

有问题的批处理代码错过了所有goto :EOFexit /B(相同)以退出当前子例程或整个批处理文件。我添加了命令exit /B以退出批处理文件的处理,并添加goto :EOF以退出每个子例程。

TMP 是预定义的环境变量,例如 TEMP ,其中包含临时文件的文件夹路径。因此,环境变量 TMP 的值不应被不同的东西覆盖,因为如果使用的控制台应用程序或命令之一依赖于 TMP 期望它包含它,则可能导致意外行为临时文件的文件夹路径。上面的批处理代码使用环境变量TempXmlFile。在命令提示符窗口中运行命令set以显示预定义的环境变量及其当前值。

临时文件的文件夹通常包含空格。没有或包含空格或其中一个字符&()[]{}^=;!'+,`~的路径的每个文件/文件夹名称必须用双引号括起来才能被解释正确。因此,每个引用环境变量 TEMP TMP 的文件/文件夹路径都必须用双引号括起来才能正确解释。

传递给子例程FindReplace的第三个参数字符串是要修改的文件的名称或文件名模式,如果它包含空格或其他特殊字符,则必须用双引号括起来。因此,在命令 DIR 上使用"%3"作为已用双引号括起来的文件名导致文件名周围有两个双引号是不好的。因此更好的是"%~3"因为%~3在执行时被第三个参数字符串替换而没有包含双引号。

2^>nul是打印错误消息的重定向,用于处理 STDERR 到设备 NUL 以通过转义redirection operator {{1来禁止它们将} >应用于运行命令 DIR ,而不是在 FOR 命令行上进行解释。如果根据第三个参数字符串找不到任何文件,则添加的重定向会抑制命令 DIR 输出的错误消息。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • ^
  • call /?
  • cscript /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • exit /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • set /?

另请参阅Microsoft文章Using command redirection operators

使用Windows命令行替换文件中文本的其他解决方案可在以下位置找到:
How can you find and replace text in a file using the Windows command-line environment?

相关问题