簡體   English   中英

查找並替換腳本后運行bat命令

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

我正在使用以下腳本來查找和替換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

在這部分之后,我還有一些命令要運行,但是執行上述部分后,批處理文件將退出。

您能否建議我在以上部分之后繼續執行腳本的任何方法?

顯然,在不真正理解該代碼的情況下,從其他來源一起復制的批處理代碼存在許多小錯誤。 以下固定代碼適用於名稱為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以顯示預定義的環境變量及其當前值。

臨時文件文件夾通常包含空格。 每個不帶路徑或帶路徑的文件/文件夾名稱都包含空格或以下字符之一( &()[]{}^=;!'+,`~必須用雙引號引起來才能正確解釋。 因此,每個引用環境變量TEMPTMP的文件/文件夾路徑都必須用雙引號引起來,以便正確解釋。

傳遞給子例程FindReplace的第三個參數字符串是要修改的文件的名稱,或者文件名模式,如果該文件名包含空格或其他特殊字符,則必須用雙引號引起來。 因此,在命令DIR上使用"%3"是不好的,因為已經用雙引號引起來的文件名會在文件名周圍產生兩個雙引號。 因此, "%~3" 〜3”要好得多,因為在執行時, %~3 〜3被第三個參數字符串替換而沒有雙引號引起來。

2^>nul是將打印為處理STDERR的錯誤消息重定向到設備NUL的錯誤消息,以通過將重定向操作符 >^轉義來抑制錯誤消息,這些錯誤消息將應用於運行命令DIR上 ,而不在FOR命令行上進行解釋。 如果找不到根據第三個參數字符串的任何文件,此添加的重定向將抑制命令DIR輸出的錯誤消息。

為了了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並非常仔細地閱讀每個命令顯示的所有幫助頁面。

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

另請參閱Microsoft文章“ 使用命令重定向運算符”

使用Windows命令行替換文件中文本的其他解決方案可以在以下位置找到:
如何使用Windows命令行環境查找和替換文件中的文本?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM