2

I need to remove a string that may occur inside a file. The string has many lines. Can I perform this action using a Batch script?

I've heard that you cant have variables with more than one line in Batch? The string will come from another file that I will read into a variable using Batch.

The following code seems to only store the 1st/last line of a file in the string?? Whats going wrong?

Rem Read file and store all contents in string
Set replace=
Set target=
Set OUTFILE=res.txt
for /f "delims=" %%i in (myFile.txt) do set target=%target% %%i

echo %target%
Rem When I print target I only have one line not many lines?? Whats going wrong

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
for /f "tokens=1,* delims=¶" %%A in ( '"type myOtherFile.txt"') do (
SET string=%%A
SET modified=!string:%target%=%replace%!

echo !modified! >> %OUTFILE%
)
sazr
  • 24,984
  • 66
  • 194
  • 362
  • You can use the helper batch file `findrepl.bat` with the /v switch and remove a block of text from a file by specifying the start and end terms. – foxidrive Jan 31 '14 at 07:45

2 Answers2

4

Try this:

@echo off &setlocal enabledelayedexpansion
for /f "delims=" %%i in (myFile.txt) do set "target=!target! %%i"
echo %target%

Inside a code block you need always delayed expansion for variables with variable values.

Endoro
  • 37,015
  • 8
  • 50
  • 63
0

Try this, change last to:

echo !target!
Max
  • 12,622
  • 16
  • 73
  • 101
Amar Kumbhar
  • 331
  • 3
  • 11