0

I am attempting to remove a multi-line string from a file using Batch in Windows. My batch code is doing some weird things such as place the string ECHO is off. in the file.

Edit: with all code
How can I get my code to remove a multi-line string from a file?

@echo off &setlocal enabledelayedexpansion

Rem Read file and store all contents in string
Set replace=
Set target=
Set infile=usermenuTest1.4d
Rem %~1
Set outfile=usermenuTest2.4d
Rem %~2

for /f "delims=" %%i in (%infile%) do set "target=!target! %%i"
echo %target%

Rem Remove the target string from myOtherFile.txt: this code is from http://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,* delims=¶" %%A in ( 'type "%outfile%"') do SET "string=%%A"
SET "modified=!string:%target%=%replace%!"
(echo(%modified%)>> "%outfile%"

Example of the multi-line string I want to replace:

Menu "User" {
   Button "" {
      Walk_Right ""
   }
}

Menu "" {
   // Button "CAD" {
   //   Walk_Right "CAD"    
   //   }
    Button "Design" {
      Walk_Right "Design"    
      }
    Button "Services" {
      Walk_Right "Services"        
      }
    Button "Strings" {
      Walk_Right "Strings"        
      }
    Button "Survey" {
      Walk_Right "Survey"        
      }
    Button "Utilities" {
            Walk_Right "Utilities"        
      }
    Button "Zoom" {
      Walk_Right "Zoom"        
      }        
  }
sazr
  • 24,984
  • 66
  • 194
  • 362

1 Answers1

0

Try this:

@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,* delims=¶" %%A in ( 'type "%outfile%"') do SET "string=%%A"
SET "modified=!string:%target%=%replace%!"
(echo(%modified%)>> "%outfile%"
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • that sortof worked, the `Echo is off` is now not there but the string I want to remove is still present in the file when it shouldn't be. – sazr Apr 30 '13 at 04:41
  • I've edited my question with the complete code that shows all variables – sazr Apr 30 '13 at 05:03
  • The `for /f` loop reads this line by line. It can't put `CR/LF` into the string variable. – Endoro Apr 30 '13 at 05:34
  • does that mean I can replace a multiline string inside a file using this for loop/batch code? – sazr Apr 30 '13 at 05:44
  • Every line in your file is a string. You can display the string with the following code: `for /f "delims=" %%A in ( 'type "%outfile%"') do echo(%%A`. The for loop doesn't put the "end of line" sign (CR/LF) in a string. – Endoro Apr 30 '13 at 05:48