我需要一个批处理文件:
在user.cfg文件中检查字符串" g_language = Russian"并留下它 找到它但是如果将sting设置为" g_language = English"然后将其设置为 " g_language =俄语"
等待some.exe启动及启动时
更改字符串" g_language =俄语"到" g_language =英语"
我该怎么做?
我使用了这段代码,但结果是:
Russian=g_language = English= Russian
@echo off &setlocal
set "search=g_language = Russian"
set "replace=g_language = English"
set "textfile=user.cfg"
set "newfile=user.bak"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%`
答案 0 :(得分:0)
@echo off &setlocal
set "Russian=g_language = Russian"
set "English=g_language = English"
set "textfile=user.cfg"
set "newfile=user.bak"
Call :SwapLang Russian
Start "" some.exe
Timeout /t 5
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
答案 1 :(得分:0)
这是对已经给出的好答案的替代方案。
我还没有能够对此进行测试,(请先在测试环境中试用),只有在您觉得自己会从稍微不同的方法中受益时才会这样做。< / p>
@Echo Off
Set "fexe=some.exe"
Set "fcfg=user.cfg"
Set "fbak=user.bak"
Set "sstr=g_language"
Set "rlng=Russian"
Set "elng=English"
If Exist "%fbak%" (If Not Exist "%fcfg%" (Copy "%fbak%" "%fcfg%"
) Else Copy "%fcfg%" "%fbak%") Else If Exist "%fcfg%" (Copy "%fcfg%" "%fbak%"
) Else Exit/B
QProcess "%fexe%">Nul 2>&1 &&(FindStr/IC:"%sstr% = %elng%" "%fbak%">Nul||(
Call :Sub "%rlng%" "%elng%"))||(
FindStr/IC:"%sstr% = %rlng%" "%fbak%">Nul||(Call :Sub "%elng%" "%rlng%"
Start "" "%fexe%"))
Exit/B
:Sub
(For /F Delims^= %%A In ('FindStr $ "%fbak%"') Do If /I "%%A"=="%sstr% = %~1" (
Echo %sstr% = %~2) Else Echo %%A)>"%fcfg%"
我已经做到这一点,您只需要检查/调整3
行到8
的项目。
总体思路:
user.cfg
和user.bak
都不存在,请退出。user.cfg
或user.bak
中只有一个存在,则将其中一个复制到另一个。user.cfg
和user.bak
,请将user.cfg
复制到user.bak
。some.exe
正在投放且user.cfg
g_language
不是English
,请将其更改为English
。some.exe
未运行且user.cfg
g_language
未Russian
,请将其更改为Russian
并运行some.exe
当然,如果user.cfg
更改在some.exe
重新启动后才生效,则无论如何都不重要。