2

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

我听说你不能在批处理中拥有多于一行的变量?该字符串将来自另一个文件,我将使用 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%
)
4

2 回答 2

4

试试这个:

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

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

于 2013-04-30T03:09:46.220 回答
0

试试这个,最后改成:

echo !target!
于 2014-01-31T07:41:08.643 回答