Batch check and replace string then wait for a process

拈花ヽ惹草 提交于 2019-12-13 03:27:20

问题


I need a batch file witch will:

  • check inside user.cfg file for string "g_language = Russian" and leave it if finds it but if sting is set to "g_language = English" then set it to "g_language = Russian"

  • wait for some.exe to start and when it is started

  • change string "g_language = Russian" to "g_language = English"

How can I do this?

I used this code but my result is: 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%`

回答1:


@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



回答2:


Heres an alternative to the great answer already given.

I haven't been able to test this, (please try it in a test environment first), and only do so if you feel you would benefit from the slightly different approach.

@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%"

I have made it so that you should only need to check/adjust the items on lines 3 to 8.

The general idea:

  • If neither user.cfg and user.bak exist, exit.
  • If only one of user.cfg or user.bak exists copy one to the other.
  • If both user.cfg and user.bak exist, copy user.cfg to user.bak.
  • If some.exe is running and user.cfg's g_language is not English change it to English.
  • If some.exe is not running and user.cfg's g_language is not Russian change it to Russian and run some.exe.

Of course none of this matters if user.cfg changes do not take effect until some.exe is restarted.



来源:https://stackoverflow.com/questions/45879928/batch-check-and-replace-string-then-wait-for-a-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!