1

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?

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • You're not running the batch script as an Administrator, are you? That's the only possible reason that the code that you've posted wouldn't work if jrepl.bat was in the same directory as the script like you say it is. – SomethingDark Jun 27 '18 at 18:18
  • 1
    Either ensure that the directory where `jrepl.bat` is located is in the `PATH` variable or specify the full path to `jrepl.bat` in the command. `CALL "C:\path\to\jrepl\dir\jrepl.bat"` – lit Jun 27 '18 at 19:49
  • 1
    I would write `call "%~dp0jrepl.bat"` to be sure it is found, given that it is placed in the same directory as your batch file, as you anyway claim... – aschipfl Jun 27 '18 at 20:13
  • @SomethingDark I am running as the script as Admin. @lit I used the full path and got this error `JScript runtime error in Search regular expression: Syntax error in regular expression` – Hobi Wan Kenobi Jun 27 '18 at 22:43
  • 2
    `)` is typically a group character in regex so you may need to escape it with a backslash to make it literal i.e. `set "search=false\); // disable U"` – michael_heath Jun 28 '18 at 00:54

1 Answers1

0

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143