Search code examples
batch-filecmdwindows-10jrepl

Changing a line in a cfg file with a batch file


I'm on Windows, trying to use the following batch file:

@echo off &setlocal
set "search=false); // disable U"
set "replace=true); // disable U"
set "textfile=C:\Program Files (x86)\Mozilla Firefox\mozilla.cfg"
call jrepl.bat "%search%" "%replace%" /f "%textfile%" /o -
pause

to change a line in a config file. When I run the file, it says

'jrepl.bat' is not recognized as an internal or external command.

Both my batch file and jrepl.bat are saved to the desktop. How can I get my batch to find jrepl?


Solution

  • Use this batch file code to work independent on what is the current directory on executing the batch file:

    @echo off
    if not exist "%~dp0jrepl.bat" goto :EOF
    if not exist "%ProgramFiles(x86)%\Mozilla Firefox\mozilla.cfg" goto :EOF
    
    call "%~dp0jrepl.bat" "search=false\); // disable U" "search=true); // disable U" /F "%ProgramFiles(x86)%\Mozilla Firefox\mozilla.cfg" /O -
    

    See answer on How to run tree command from current directory? and the answers referenced there for details why jrepl.bat was not found by Windows command processor on running the batch file as administrator to get write access to the file in program files folder of Mozilla Firefox.

    ) has a special meaning in a regular expression search string. It must be escaped with a backslash to get it interpreted as literal character in search string. ) does not have a special meaning in a regular expression replace string and so there is no need to escape it in replace string.