简体   繁体   中英

Batch find and replace string adds blank line

I got this code (in this question ) that finds and replaces string but it adds a blank new line at the end of the output file and makes it not usable.

@echo off &setlocal
set "Russian=g_language = Russian"
set "English=g_language = English"

set "textfile=user.cfg"
set "newfile=user.bak"

Call :SwapLang Russian

:search
Color 9B
@echo off&prompt :&mode con cols=60 lines=16


echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                      PLEASE RUN NOTEPAD
echo.
echo.
echo.
echo.
echo.

:search
TIMEOUT /T 2
TASKLIST|FIND "notepad.exe"
IF %ERRORLEVEL% equ 0 (GOTO found) ELSE (GOTO search)

:found

Call :SwapLang English

Goto :Eof

:SwapLang %1 byRef
( for /f "delims=" %%i in (%textfile%) do (
  set "line=%%i"
  setlocal EnableDelayedExpansion
  If /I "!line:~0,12!" Equ "g_language =" (
    echo(!%1!
  ) Else (
    echo(!line!
  )
  endlocal
)) > "%newfile%"
del %textfile%
rename %newfile%  %textfile%
Goto :Eof

INPUT FILE: It has 6 line with no blank line.

sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = Russian
cl_promoPageWidth = 820
cl_promoPageHeight = 726

OUTPUT1 FILE: It has 7 lines, last line is blank (can't be shown here).

sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = Russian
cl_promoPageWidth = 820
cl_promoPageHeight = 726

OUTPUT2 FILE: It has 7 lines, last line is blank(can't be shown here).

sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = English
cl_promoPageWidth = 820
cl_promoPageHeight = 726

Could someone help me with this?

Batch isn't ideal for editing files. This batch uses PowerShell to do the editing with a Regular Expression:

:: Q:\Test\2017\08\25\SO_45879928.cmd
@echo off &setlocal

set "textfile=user.cfg"

Call :SwapLang Russian

:search
Color 9B
@echo off&prompt :&mode con cols=60 lines=16
echo.
echo.
echo                      PLEASE RUN NOTEPAD
echo.
echo.
:search
TIMEOUT /T 2
TASKLIST|FIND "notepad.exe"
IF %ERRORLEVEL% equ 0 (GOTO found) ELSE (GOTO search)

:found

Call :SwapLang English

Goto :Eof

:SwapLang %1
Powershell -NoP -C "$txt=(gc %textfile% -raw) -replace '(?<=g_language = )(English|Russian)','%1';[IO.File]::WriteAllText('%textfile%', $txt)"

Goto :Eof

I've solved your original question : Batch check and replace string then wait for a process by just 2 command lines using msr.exe as following: (full message is there, with a colorful screenshot )

msr -p user.cfg -it "(g_language\\s*=\\s*)\\w*" -o "${1}Russian" -R

msr -p user.cfg -it "(g_language\\s*=\\s*)\\w*" -o "${1}English" -R

Additonaly, for your questions here , also 1 command to remove tail new lines:

msr -p your-files-dirs -S -t "[\\r\\n]+$" -o "" -R

Or remove tail white-spaces and new lines:

msr -p your-files-dirs -S -t "\\s+$" -o "" -R

According to stackoverflow rules, say again here: msr.exe / msr.gcc* / msr.cygwin etc is in my open project https://github.com/qualiu/msr tools directory, as a general tool to M atch/ S earch/ R eplace files/pipe lines/blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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