使用批处理脚本替换文本文件中的子字符串

时间:2013-12-19 01:34:52

标签: regex batch-file replace substring

我需要通过控制面板为数百个服务部署批量更新,并且需要在执行此操作时替换特定文件中的子字符串。如何使用批处理文件更改以下内容并将其重写为同一文件而不是新文件?

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

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

3 个答案:

答案 0 :(得分:1)

这是使用纯批次执行此操作的另一种方法。这会写入另一个文件,但在没有提示的情况下移动它,用更改覆盖原始文件。

@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 / Batch

的方法
@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

答案 1 :(得分:0)

试试sed for Windows

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

答案 2 :(得分:0)

您可以使用此例程将字符串替换为文件中的另一个字符串,保存在单独的文件中或覆盖原始文件,只是通过第四个参数将其保存在单独的文件中,否则,不要传递参数和将被新内容替换。

@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