2017-08-27 58 views
1

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

有人可以幫助我嗎?

+0

代碼中沒有任何東西可以產生額外的行。你的意思是「我輸入文件中的最後一行沒有終止符('CRLF')」? 'CRLF'確實使文件無法使用? – Stephan

+0

我不知道真的。 –

+0

@Stephan Echo總是以cr/lf結束一行。該文件的最後一行沒有,也不應該有cr/lf。我去了PowerShell,但它的Set-Content也一樣。只有我發現的方式是dot net的'[IO.File] :: WriteAllText()' – LotPings

回答

0

批量編輯文件不理想。該批次使用PowerShell中做編輯,正則表達式:

:: 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 
+0

結果與上面的批處理相同。我想我會放棄這種方法,並嘗試複製>重命名文件的方法。 –

+0

看起來像'sc'或'Set-Content'還會在文件[refernce](https:// stackoverflow)的末尾放置一個終止cd/lf。com/questions/30751320/powershell-replace-content-in-file-adds-redundent-carriage-return)解決方法來自我的答案中的參考答案。現在應該工作。 – LotPings

+0

@ LotPings我不得不感謝您花時間和精力回答您的問題! –

0

我替你解決原來的問題:使用msr.exe如下僅有2命令行Batch check and replace string then wait for a process:(已滿的消息是存在的,與一多彩的屏幕截圖

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,你的問題在這裏,也是1個命令刪除尾新線:

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

或者刪除尾部的空格和換行:

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

根據stackoverflow規則,在這裏再說一遍:msr.exe/msr.gcc*/msr.cygwin等是在我打開的項目https://github.com/qualiu/msrtools目錄,作爲的一般工具M atch/S earch/R替換文件/管道線路/塊。