понеділок, 4 травня 2020 р.

How to replace text in batch file

First part. What if you want to replace some text in your source code based on different settings (f.e., when CI uses)? I had a situation, when host and database were hardcoded by another person as consts and they have stay there. You may move consts into ini file (what I did some time later), but you also are able to parse your source file. Below I'll show how to do it using bat file.

So code below searches consts with the names SQL_SERVER_NAME and DATABASE_NAME and replaces them with the replace_SQL_SERVER_NAME and replace_DATABASE_NAME lines. Now you know, that this code has a limitation: it replaces whole lines. But it works, that it.

REM database.php update START
@echo off &setlocal
setlocal enableDelayedExpansion
:: Lines below has been searched
set "const=const"
set "SQL_SERVER_NAME=SQL_SERVER_NAME"
set "DATABASE_NAME=DATABASE_NAME"
:: Lines below has been replaced with the "search_xxx"
:: !!!-------------------- UPDATE VARIABLES BELOW --------------------!!!
set "replace_SQL_SERVER_NAME= const SQL_SERVER_NAME = "SERVER\NEW_SERVER_NAME";"
set "replace_DATABASE_NAME= const DATABASE_NAME = "NEW_DATABASE_NAME";"
:: !!!-------------------- UPDATE VARIABLES ABOVE --------------------!!!
:: File to be processed
set "textfile=database.php"
:: Temporary file
set "newfile=database_tmp.php"
:: Define LF to contain a linefeed character
set "signExclamation=^!"
:: Define LF to contain a linefeed character
set ^"LF=^

^"
:: The empty line above is critical
::(for /f "delims=" %%i in (%textfile%) do (
(for /f "USEBACKQ tokens=1,2 delims=^!^" %%i in (`type %textfile% ^| find /V /N ""`) do (
    set "part1=%%i"
 set "part2=%%j"
 :: Save ! sign
 if "!part2!"=="" (
  set "line=!part1!!part2!"
 ) else (
  set "line=!part1!!signExclamation!!part2!"
 )

 :: Save an empty lines
 set "line=!line:*]=!"

 set "str1="
 set "str2="
 :: Left trim the line to compare with the "search"
 for /f "tokens=1,2 delims=  " %%a in ("!line!") do (
  set "str1=%%a"
  set "str2=%%b"
 )

 :: Replace consts
 set "str=!line!"
 if "!str1!"=="!const!" (
  if "!str2!"=="!SQL_SERVER_NAME!" (
   set "str=!replace_SQL_SERVER_NAME!"
  ) else (
   if "!str2!"=="!DATABASE_NAME!" (
    set "str=!replace_DATABASE_NAME!"
   )
  )
 )

    echo(!str!)
)>"%newfile%"
del %textfile%
rename %newfile%  %textfile%
endlocal
@echo on
REM database.php update END

You are welcome to test code above. Just create database.php file, add there 2 lines:
 const SQL_SERVER_NAME = "OLD_SERVER_NAME";
 const DATABASE_NAME = "OLD_DATABASE_NAME";
Create *.bat file in the same folder and run it.

Second part. What if you want to insert some code into your f.e. web.config file in production?

REM web.config update START
@echo off &setlocal
setlocal enableDelayedExpansion
:: Line below has been searched
set "search=</system.webServer>"
:: Line below has been added before the "search"
set "replace=        <urlCompression doStaticCompression="false" doDynamicCompression="false" />"
:: File to be processed
set "textfile=web.config"
:: Temporary file
set "newfile=web_tmp.config"
:: Define LF to contain a linefeed character
set ^"LF=^

^"
:: The empty line above is critical
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
 :: Left trim the line to compare with the "search"
 for /f "tokens=* delims= " %%a in ("!line!") do set "str=%%a"
 :: Add line before the "search"
 if "!str!"=="!search!" (
  set "str=!replace!!LF!"
 ) else (
  set "str="
 )
    echo(!str!!line!))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%
endlocal
@echo on
REM web.config update END

Well, it just finds search in in your web.config file and inserts replace before it. You know how to test.

Немає коментарів:

Дописати коментар