The exclamation mark is permanently deleted in the batch file.

Someone, please help ... oh, I will be so grateful. I have a very long batch file that works fine, except that every time the user enters input, it replaces the lines in the desired files, BUT it deletes! S in the files, causing problems due to the fact that they are XML configuration files, and they are commented out which should remain. I won’t insert all the code here unless requested, but in the walnut shell the user enters certain inputs and then runs the batch file ... here is part of the code for one file .... the user enters the installation letter and bdi server name into the drive . I want user input to replace% drive% and% bdi1% .... what it does .... but I DO NOT want it to replace commented partitions ... ie ::

<!-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message. turns into <-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message.

notice without!

here is my code ... what i need to do to stop it! I tried looking here and I thought that I was well on the road with Jeb's answers, but I could not get him to work. thank you in advance

if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config



if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:bdi_name=%bdi1%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config



if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:share_name=%share%$!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config


if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:drive_bdi=%drive%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
+2
source share
1 answer

This is the effect of delayed expansion and a packet parser.
If the delayed extension is disabled, there is no problem with exclamation marks, but if it allows the parser to assume that it is !intended to expand the variable, and when there is only one mark, it will be deleted.

So the solution is to turn off slow expansion, but it should also be turned on as needed!

, .

setlocal DisableDelayedExpansion
(
  for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
    set "str=%%a"
    setlocal EnableDelayedExpansion
    set "str=!str:server_name=%server%!"
    set "str=!str:bdi_name=%bdi1%!"
    set "str=!str:share_name=%share%$!"
    set "str=!str:drive_bdi=%drive%!"
    echo(!str!
    endlocal
  )
) > newfile.txt

rediretion FOR-, , .
, .

+7

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


All Articles