2

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
Robby Johnston
  • 183
  • 3
  • 7
  • 15

1 Answers1

8

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.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • that helped condense it, but it unfortunately just created a newfile.txt and didnt do anything to the original file...and the new file.txt doesnt look correct unfortunately – Robby Johnston Oct 25 '12 at 13:16
  • _the new file.txt doesnt look correct unfortunately_ is a bit vague. A bit more information could be useful. Btw. The batch doesn't try to rename/copy the newfile.txt to the original.file, as this isn't the problem here – jeb Oct 25 '12 at 13:23
  • Ok, np, i can do the rename and all that....but the newfile.txt looks like this (excerpt): – Robby Johnston Oct 25 '12 at 13:31
  • c:\McKesson\_test>( set "str=" setlocal EnableDelayedExpansion set "str=!str:server_name=atlcs401!" set "str=!str:bdi_name=_test!" set "str=!str:share_name=global151$$!" set "str=!str:drive_bdi=c!" echo(!str! endlocal ) c:\McKesson\_test>( set "str= – Robby Johnston Oct 25 '12 at 13:31
  • it is hard to put it in here and format it,but basically, it is not replacing the values, and is adding the literal code into the new.txt – Robby Johnston Oct 25 '12 at 13:31
  • Did you begin your batch file with `@ECHO OFF`? – jeb Oct 25 '12 at 13:38
  • Jeb, you are a genius....what do you do for a living bc you surely have helped me a lot on my project. THanks so much!! – Robby Johnston Oct 25 '12 at 13:41