Windows Batch Files - Using Exclamation Marks in Variables Without Disabling Slow Extension

I have a script package that replaces multiple occurrences of the source string in the source text file with a custom input string.
To do this, I use a slightly corrected version of this answer here: How to replace substrings in a Windows batch file
(This is not an accepted answer, if accepted it changes the source file even more!) But I could only resolve a bit of “adds a space to each line” - an error, and not the main problem of the inability to use source files with exclamation marks inside!

For this remaining problem, I investigated a few more and, for example, found (and tried the sentences) the following topics:
- The exclamation mark is permanently deleted in the batch file
- Windows package - The deferred extension removes the exclamation mark
Unfortunately, solutions to these issues (temporarily disabling the "deferred extension ") do not apply to me, as there is one line in my script that needs a" delayed extension "and should handle variables with potential exclamation points!

This is a line
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
that can be seen in its context after my first link.
As a brief explanation: this line requires the extension to be functional because it is inside an if-else block inside a block. On the other hand, the value of the variable "string" may contain exclamation points, as it scans the source file line by line.

Does anyone have an idea how I can solve this?

EDIT:
According to the first comments, I tried to enable deferred extensions only for the necessary parts of the main loop (only where access to the contents of the modified variable is necessary using the "!" Operator). The entire main loop now looks like this:


    setlocal disabledelayedexpansion
    for /f "tokens=1,* delims=¶" %%A in ('"findstr /n ^^ %INTEXTFILE%"') do (
      SET string=%%A
      setlocal enabledelayedexpansion
      for /f "delims=: tokens=1,*" %%A in ("!string!") do set "string=%%B"
      if  "!string!" == "" (
        echo.>>%OUTTEXTFILE%
      ) else (
        SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
        echo !modified!>> %OUTTEXTFILE%
      )
      endlocal
    )
    endlocal

, . , script.
? ( , .)

+4
1

.

findstr/n, .
, , .

for /f "delims=: tokens=1,*" %%A in ("!string!") do set "string=%%B"

, .

set "string=!string:*:=!" Remove all up to the first colon

- .
echo. .
echo !variable! , variable

,

setlocal disabledelayedexpansion
for /f "tokens=* delims=" %%A in ('"findstr /n ^^ %INTEXTFILE%"') do (
  SET "string=%%A"
  setlocal EnableDelayedExpansion
  set "string=!string:*:=!"
  if defined string (
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
  )
    (echo(!modified!) >> %OUTTEXTFILE%
  endlocal
)
endlocal

- , - .
, , , , .

(echo Sample)    >> myOutputFile.txt

, ECHO , . .

set "var1=/?"
set "var2=    "
set "var3=ON"
echo %var1%
echo %var2%
echo %var3%

echo( . , .

echo(!variable! (echo(!variable!), , echo.

+1

Source: https://habr.com/ru/post/1612775/


All Articles