在批处理变量中存储多行字符串

时间:2013-04-30 02:56:53

标签: windows batch-file

我需要删除文件中可能出现的字符串。该字符串有很多行。 我可以使用批处理脚本执行此操作吗?

我听说您在批处理中不能有多行变量?该字符串将来自另一个文件,我将使用Batch读入变量。

以下代码似乎只存储字符串中文件的第一行/最后一行?怎么回事?

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%
)

2 个答案:

答案 0 :(得分:3)

试试这个:

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

在代码块中,对于具有变量值的变量,您始终需要delayed expansion

答案 1 :(得分:0)

试试这个,最后改为:

echo !target!