我得到了此代碼(在此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