簡體   English   中英

用批處理腳本替換文本文件中的子字符串

[英]Replace substring in text file with a batch script

我需要通過控制面板將批量更新部署到數百個服務,並且在執行此操作時需要替換特定文件中的子字符串。 如何使用批處理文件更改以下內容並將其重寫為同一文件而不是新文件?

hostName =“ Server UK(Public / Veteran)116621”;

hostName =“ Server UK(Public / Veteran)224421”;

這是使用純批處理的另一種方法。 這確實會寫入另一個文件,但隨后會在不提示的情況下將其移動,並用更改覆蓋原始文件。

@echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%f in (file.cfg) do if not "%%f"=="" (
        set "line=%%f"
        set "line=!line:116621=224421!"
        echo(!line!
)) > tmp.cfg
Move /y tmp.cfg file.cfg

這是混合VBS /批處理的一種方法

@echo off
setlocal

call :FindReplace "116621" "224421" file.cfg
exit /b 

:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
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
exit /b

: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

只需為Windows嘗試sed

for %%a in (*.txt) do sed -ri.bak "s#hostName = \"Server UK \(Public/Veteran\) 116621\"#hostName = \"Server UK (Public/Veteran) 224421\"#g" "%%~a"

您可以使用此例程在文件中用另一個字符串替換字符串,保存在單獨的文件中或覆蓋原始字符串,僅在第四個參數之后將其保存在單獨的文件中,否則,請不要傳遞參數並將其替換新內容。

@echo off
@break off
@title Replace substring in text file with a batch script - D3F4ULT
@color 0a
@cls

:: Routine to replace an input string by a string output in the contents of a file.

setlocal EnableDelayedExpansion

REM Examples

REM call:replace-string-file-from-to "config.cfg" "116621" "224421"
REM call:replace-string-file-from-to "config.cfg" "116621" "224421" "config.cfg"
REM call:replace-string-file-from-to "config.cfg" "116621" "224421" "configuration.dat"
REM call:replace-string-file-from-to "%UserProfile%\Downloads\config.cfg" "116621" "224421" "%UserProfile%\Downloads\configuration.dat"

REM Calling Routine
call:replace-string-file-from-to "config.cfg" "116621" "224421"

exit

:replace-string-file-from-to

:: %~1 = File [In]  [Mandatory]
:: %~2 = From [In]  [Mandatory]
:: %~3 = To   [In]  [Mandatory]
:: %~4 = Out  [Out] [Optional]

REM Mandatory Parameters
if "%~1" EQU "" echo Error %%~1 not defined&&echo.&&ping -n 4 localhost>nul&&exit /B 1
if "%~2" EQU "" echo Error %%~2 not defined&&echo.&&ping -n 4 localhost>nul&&exit /B 1
if "%~3" EQU "" echo Error %%~3 not defined&&echo.&&ping -n 4 localhost>nul&&exit /B 1

if exist "%~dpnx1" (
  set "INPUT_FILE_LOCATION=%~dpnx1"
  cd /D "%~dp1"
) else if exist "%~dp0\%~nx1" (
  set "INPUT_FILE_LOCATION=%~dp0%~nx1"
  cd /D "%~dp0"
) else if exist "!CD!\%~nx1" (
  set "INPUT_FILE_LOCATION=!CD!\%~nx1"
  cd /D "!CD!"
)

if not defined INPUT_FILE_LOCATION (
  echo ERROR
  echo.
  echo File not found = '%~nx1'
  echo.
  echo Exiting...
  echo.
  ping -n 8 localhost>nul
  exit /B 1
)

set "INPUT_FILE_FOLDER=%~dp1"
set "INPUT_FILE_NAME=%~n1"
set "INPUT_FILE_EXTENSION=%~x1"

if "%~4" EQU "" (
  set "OUTPUT_FILE_LOCATION=!INPUT_FILE_FOLDER!!INPUT_FILE_NAME!__new__!INPUT_FILE_EXTENSION!"
  set "INOUT_DEL_MOVE=true"
) else if "%~dpnx1" EQU "%~dpnx4" (
  set "OUTPUT_FILE_LOCATION=!INPUT_FILE_FOLDER!!INPUT_FILE_NAME!__new__!INPUT_FILE_EXTENSION!"
  set "INOUT_DEL_MOVE=true"
) else (
  set "OUTPUT_FILE_FOLDER=%~dp4"
  set "OUTPUT_FILE_NAME=%~n4"
  set "OUTPUT_FILE_EXTENSION=%~x4"
  set "OUTPUT_FILE_LOCATION=!OUTPUT_FILE_FOLDER!!OUTPUT_FILE_NAME!!OUTPUT_FILE_EXTENSION!"
)

if exist "!OUTPUT_FILE_LOCATION!" (
  del /f /q "!OUTPUT_FILE_LOCATION!">nul
)

for /F "usebackq tokens=*" %%A in ( "!INPUT_FILE_LOCATION!" ) do (
  set "string=%%~A"
  set "string=!string:%~2=%~3!"
  echo !string!>>"!OUTPUT_FILE_LOCATION!"
)

if defined INOUT_DEL_MOVE (
  if exist "!INPUT_FILE_LOCATION!" (
    del /f /q "!INPUT_FILE_LOCATION!">nul
  )
  if exist "!OUTPUT_FILE_LOCATION!" (
    move /y "!OUTPUT_FILE_LOCATION!" "!INPUT_FILE_LOCATION!">nul
  )
)

goto:eof

暫無
暫無

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

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