简体   繁体   English

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

[英]Running a bat command after find and replace script

I'm using the below script to find and replace a text in XML 我正在使用以下脚本来查找和替换XML中的文本

setlocal

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

:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.xml"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
  for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
    echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
    <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
    if exist %tmp% move /Y %tmp% "%%~dpnxa">nul

  )
)

del %temp%\_.vbs

: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

I also have a few commands to run after this part, but the batch file exits after executing the above part. 在这部分之后,我还有一些命令要运行,但是执行上述部分后,批处理文件将退出。

Can you suggest me any way to make the script continue after the above part? 您能否建议我在以上部分之后继续执行脚本的任何方法?

The batch code copied obviously together from other sources without really understanding the code has many small mistakes. 显然,在不真正理解该代码的情况下,从其他来源一起复制的批处理代码存在许多小错误。 The fixed code below works for a file with name new-text . 以下固定代码适用于名称为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 is a strange name for a file or file name pattern. new-text是文件或文件名模式的奇怪名称。 I suppose the code should run on a *.xml file and therefore the name of this XML file should be used instead of new-text . 我想代码应该在* .xml文件上运行,因此应该使用此XML文件的名称而不是new-text

The batch code in question miss all goto :EOF or exit /B (are identical) to exit the current subroutine or the entire batch file. 有问题的批处理代码缺少所有goto :EOFexit /B (相同)以退出当前子例程或整个批处理文件。 I added command exit /B to exit processing of batch file and goto :EOF to exit each subroutine. 我添加了命令exit /B退出批处理文件的处理,并添加了goto :EOF退出每个子例程。

TMP is a predefined environment variable like TEMP which contains the path to folder for temporary files. TMP是一个预定义的环境变量,例如TEMP ,它包含临时文件文件夹的路径。 Therefore value of environment variable TMP should not be overwritten by something different as this could result in unexpected behavior if one of the used console applications or commands depends on TMP expecting that it contains the path to folder for temporary files. 因此,环境变量TMP的值不应被不同的东西所覆盖,因为如果使用的控制台应用程序或命令之一依赖于TMP期望它包含临时文件文件夹的路径,则可能导致意外行为。 The batch code above uses instead the environment variable TempXmlFile . 上面的批处理代码改为使用环境变量TempXmlFile Run in a command prompt window the command set to get displayed the predefined environment variables with their current values. 在命令提示符窗口中运行命令set以显示预定义的环境变量及其当前值。

The folder for temporary files usually contains spaces. 临时文件文件夹通常包含空格。 Each file/folder name without or with path containing a space or one of these characters &()[]{}^=;!'+,`~ must be enclosed in double quotes to be interpreted correct. 每个不带路径或带路径的文件/文件夹名称都包含空格或以下字符之一( &()[]{}^=;!'+,`~必须用双引号引起来才能正确解释。 Therefore every file/folder path with a reference to environment variable TEMP or TMP must be enclosed in double quotes to be correct interpreted. 因此,每个引用环境变量TEMPTMP的文件/文件夹路径都必须用双引号引起来,以便正确解释。

The third parameter string passed to subroutine FindReplace is the name of the file to modify or the file name pattern which must be enclosed in double quotes if it contains a space or another special character. 传递给子例程FindReplace的第三个参数字符串是要修改的文件的名称,或者文件名模式,如果该文件名包含空格或其他特殊字符,则必须用双引号引起来。 Therefore it is not good to use "%3" on command DIR as a file name already enclosed in double quotes results in two double quotes around the file name. 因此,在命令DIR上使用"%3"是不好的,因为已经用双引号引起来的文件名会在文件名周围产生两个双引号。 Much better is therefore "%~3" because %~3 is replaced on execution by third parameter string without surrounding double quotes. 因此, "%~3" 〜3”要好得多,因为在执行时, %~3 〜3被第三个参数字符串替换而没有双引号引起来。

2^>nul is a redirection for error messages printed to handle STDERR to the device NUL to suppress them with escaping the redirection operator > with ^ to be applied on running command DIR and not being interpreted on FOR command line. 2^>nul是将打印为处理STDERR的错误消息重定向到设备NUL的错误消息,以通过将重定向操作符 >^转义来抑制错误消息,这些错误消息将应用于运行命令DIR上 ,而不在FOR命令行上进行解释。 This added redirection suppresses the error message output by command DIR if it could not find any file according to third parameter string. 如果找不到根据第三个参数字符串的任何文件,此添加的重定向将抑制命令DIR输出的错误消息。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully. 为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

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

And see also the Microsoft article Using command redirection operators . 另请参阅Microsoft文章“ 使用命令重定向运算符”

Other solutions for replacing text in a file using Windows command line can be found at: 使用Windows命令行替换文件中文本的其他解决方案可以在以下位置找到:
How can you find and replace text in a file using the Windows command-line environment? 如何使用Windows命令行环境查找和替换文件中的文本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM