如何使用*或?使用repl.bat更新文本文件期间批处理文件中的字符?

时间:2017-11-13 05:21:39

标签: batch-file batch-rename

@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;

批处理文件应与jre1.8.0_121jre1.8.0_152等匹配。

所以我想将jre1.8.0_?替换为jre1.8.0_156,但?*都不能替换。

替换在删除此通配符时可以正常工作。

1 个答案:

答案 0 :(得分:1)

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.