exclamation point being removed constantly in batc

2019-02-26 09:03发布

问题:

Someone please help...oh, I will be so grateful. I have a very long batch file that is working perfectly except that every time the user enters input, it replaces strings in the files desired BUT it is removing the !s in the files causing issues due to them being XML config files and these are commented out sections that need to stay. I wont put the whole code in here unless requested, but in a nut shell, the user makes certain inputs and then the batch file runs...here is the part of the code for one file....the user enters the drive letter of the install and the name of the bdi server. I want the user input to replace %drive% and %bdi1%....which it does....but I do NOT want it to replace commented out sections...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 the without !

here is my code...what do i need to do to get it to stop removing the !. I tried looking on here and i thought i was well on my way with Jeb's answers but i couldnt get it to work. thanks 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

回答1:

It's an effect of delayed expansion and the batch parser.
If delayed expansion is disabled there aren't problems with exclamation marks, but if it's enabled the parser suppose that the ! are for expanding a variable, and when there is only one mark, it will be dropped.

So the solution is to disable delayed expansion, but as you need it, you must enable it too!

This can be done with simply toggling it in the right moment.

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

I move the rediretion to the complete FOR-block, it's faster and you don't need to delete the file first.
I try to move all your replacements into one block, so you only need to read the file only once.