0

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%`
Mac Maca
  • 13
  • 5
  • 1
    You can do it by researching and then creating a script. If once you've created that script it doesn't work as intended then you can return here with a properly formed question, showing what you've done, what happened and what your intention was. – Compo Aug 25 '17 at 11:20
  • I already did researching and everything I found and used turns out to be useless because string contains `=` – Mac Maca Aug 25 '17 at 11:36
  • You should still show those efforts otherwise your question is "I want somebody to work for me for free whilst I make no effort". – Compo Aug 25 '17 at 11:39
  • You are right. Should I ask new question or edit this one? – Mac Maca Aug 25 '17 at 11:41
  • 1
    Always use the edit facility if the question does not change significantly. – Compo Aug 25 '17 at 11:42

2 Answers2

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
  • It works only if I replace lines `Start "" some.exe Timeout /t 5 ` with `pause` but it adds a blank new line at the end of file and makes a file not usable. Also some.exe should not be started from batch but batch should wait for some.exe to start because I have to run it from it's launcher . – Mac Maca Aug 26 '17 at 12:22
  • I replaced `Start "" some.exe Timeout /t 5` with `:search TASKLIST|FIND "notepad.exe" IF %ERRORLEVEL% equ 0 (GOTO found) ELSE %ERRORLEVEL% equ 1 (GOTO search) TIMEOUT /T 2 :found` and it works fine but I am getting blank new line. How to solve it? – Mac Maca Aug 26 '17 at 21:23
0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • I can't figure it out how to modify your code because it calls some.exe to start instead to wait for process to be started cause I have to start it from it's launcher. – Mac Maca Aug 26 '17 at 12:23
  • Would just removing ` Start "" "%fexe%"` from line 17 and moving the 2 parentheses up to line 16 not do that for you? – Compo Aug 26 '17 at 14:46