我得到了此代码(在此question),它查找并替换字符串,但它在输出文件的末尾添加一个空白的新行,并使其不可用。批量查找和替换字符串添加空白行
@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
输入文件:它有没有空行6行。
sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = Russian
cl_promoPageWidth = 820
cl_promoPageHeight = 726
OUTPUT1 FILE:它有7行,最后行是空的(在这里不能示出)。
sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = Russian
cl_promoPageWidth = 820
cl_promoPageHeight = 726
OUTPUT2 FILE:它有7行,最后行是空的(在这里不能示出)。
sys_usefrontline = 1
net_frontline_address = s1.gmru.net
net_frontline_port = 5050
g_language = English
cl_promoPageWidth = 820
cl_promoPageHeight = 726
有人可以帮助我吗?
代码中没有任何东西可以产生额外的行。你的意思是“我输入文件中的最后一行没有终止符('CRLF')”? 'CRLF'确实使文件无法使用? – Stephan
我不知道真的。 –
@Stephan Echo总是以cr/lf结束一行。该文件的最后一行没有,也不应该有cr/lf。我去了PowerShell,但它的Set-Content也一样。只有我发现的方式是dot net的'[IO.File] :: WriteAllText()' – LotPings