商店多行字符串在批次变量

问题描述:

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

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

下面的代码似乎只存储字符串的文件的第一个/最后一行?发生什么事了?

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

您可以使用辅助批处理文件与'/ v开关findrepl.bat',并通过指定的起点和终点方面从文件中删除文本块。 – foxidrive 2014-01-31 07:45:28

试试这个:

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

里面你总是需要delayed expansion与变量值变量的代码块。

试试这个,改变最后:

echo !target!