Windows command processor cmd.exe
is designed for executing commands and applications. It is not designed for file content modification purposes independent on type of file.
There are lots of script interpreters which have built-in support for modification of file contents like VBScript, JScript, PowerShell, Perl, Python, ... So best would be to use a different script interpreter than Windows command processor for this task, especially on search or replace string contain "<=>|
which makes a file content modification with pure Windows command processor commands a nightmare.
However, this is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
@echo off
if not exist ".\index.html" goto :EOF
if not exist "%~dp0jrepl.bat" goto :EOF
call "%~dp0jrepl.bat" "[\t ]*<script src=\x22https://d1tdp7z6w94jbb.cloudfront.net/js/jquery-3.3.1.min.js\x22 type=\x22text/javascript\x22 integrity=\x22sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=\x22 crossorigin=\x22anonymous\x22></script>[\t ]*\r?\n?" "" /M /F ".\index.html" /O -
The batch file first checks if there is an index.html
file in current directory and immediately exits if this condition is not true, see Where does GOTO :EOF return to?
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks next if JREPL.BAT really exists in directory of the batch file and exits if this condition is not true.
Next the batch file calls JREPL.BAT to do a case-sensitive regular expression replace with replace string being an empty string.
The search string is mainly the string which should be removed from the file.
Each "
in search string is replaced by \x22
which is an expression to search for a character with hexadecimal code value 22
which is the code value of character "
to be able to specify this string on Windows command line as one argument string enclosed in double quotes.
The main search string does not contain any character with a special regular expression meaning and therefore no other character must be escaped with a backslash to be interpreted as literal character by regular expression function of JScript.
The main search string also does not contain any character with a special Windows command processor meaning even inside a double quoted argument string like percent sign %
. Each %
inside the searched string would be needed to be escaped with one more %
to be interpreted as literal character by cmd.exe
parsing this command line before calling the other batch file with the already parsed arguments.
The search expression starts with [\t ]*
to remove additionally 0 or more horizontal tabs or normal spaces left to the string to remove. The string to remove is usually in an HTML file on a separate line indented with tabs or spaces and the goal is to remove also those indenting whitespaces.
The search expression ends with [\t ]*\r?\n?
to remove additionally 0 or more horizontal tabs or normal spaces right to the string to remove, i.e. trailing whitespaces on the line, and one carriage return if existing at all, and one line-feed if existing at all.
So an entire line is removed from the file if the string to remove is on a separate line in the HTML file without or with leading tabs/spaces and without or with trailing tabs/spaces. But if the string to remove is on a line with other HTML tags, just the searched string and the tabs/spaces left and right to this string are removed from the HTML file. The JREPL.BAT option /M
is used to be able to remove an entire line and not only the searched string within the line and leaving back an empty line on script tags being on a separate line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains also %~dp0
... drive and path of argument 0 being the batch file itself.
echo /?
goto /?
if /?
jrepl.bat /?