Search code examples
batch-filebatch-rename

How to use * or ? character in batch file during update of a text file using repl.bat?


@echo off & setlocal
set "search=jre1.8.0_?"
set "replace=jre1.8.0_156"
set "textfile=C:\Program Files\ABC\_ABC_installation\Uninstall_ABC.lax"
set "newfile=C:\Program Files\ABC\_ABC_installation\Uninstall_ABC1.lax"
pause;
call repl.bat "%search%" "%replace%" L < "%textfile%" >"%newfile%"
pause;
del "%textfile%"
rename "%newfile%" "%textfile%"
pause;

The batch file should match anything like jre1.8.0_121 or jre1.8.0_152.

So I want to replace jre1.8.0_? with jre1.8.0_156, but neither ? nor * works on replace.

The replace works fine on removing this wildcard character.


Solution

  • Deprecated repl.bat as well as JREPL.BAT use JScript which offers regular expression support.

    In ECMAScript/JavaScript/JScript/Perl regular expression syntax \d+ or [0-9]+ matches 1 or more digits, i.e. a positive integer number.

    So the command line to use in the batch file is one of those 2 lines on using jrepl.bat:

    call jrepl.bat "jre1\.8\.0_\d+" "jre1.8.0_156" /F "%ProgramFiles%\ABC\_ABC_installation\Uninstall_ABC.lax" /O -
    call jrepl.bat "jre1\.8\.0_[0-9]+" "jre1.8.0_156" /F "%ProgramFiles%\ABC\_ABC_installation\Uninstall_ABC.lax" /O -
    

    Or use one of those two lines on still using repl.bat.

    set "search=jre1\.8\.0_\d+"
    set "search=jre1\.8\.0_[0-9]+"
    

    The . has a special meaning in a regular expression search string. It means by default any character except newline characters (according to Unicode standard). To get . interpreted as literal character, it must be escaped with a backslash.

    The option L must be removed on usage of repl.bat as the search string is not anymore a string to find literally, but a regular expression string.

    The batch code using deprecated repl.bat can't work as long as input and output file have same name. And even on using for output file a different name, the posted batch code does not work because command rename requires that the new file name is specified without path.
    The command move can be used to replace input file by modified output file.

    The entire batch code really working using deprecated repl.bat.

    @echo off & setlocal
    set "search=jre1\.8\.0_\d+"
    set "replace=jre1.8.0_156"
    set "textfile=%ProgramFiles%\ABC\_ABC_installation\Uninstall_ABC.lax"
    set "newfile=%ProgramFiles%\ABC\_ABC_installation\Uninstall_ABC.tmp"
    call repl.bat "%search%" "%replace%" <"%textfile%" >"%newfile%"
    move /Y "%newfile%" "%textfile%"
    

    Hint: Do not use a semicolon after command pause. There is absolute no need for a semicolon. The semicolon after pause just tests the error correction handling of Windows command interpreter.