【发布时间】:2016-04-26 08:29:56
【问题描述】:
即使经过深入的谷歌搜索,我也无法解决我的问题。 有一个名为 test.txt 的文本文件。我需要的是更改以单词“Root:”开头的行以及其他内容 - 使用批处理文件。
setLocal EnableDelayedExpansion
FINDSTR /B Root: test.txt
::returns the correct line - works well
for /f %%i in ('FINDSTR /B Root: test.txt') do set root=%%i
echo %root%
::echos "Root:" - instead of the line content
FOR /F "tokens=*" %%G IN (test.txt) DO
(set x=%%G
if !x!==%root% set x=Hello
echo !x! >> test.txt)
::The syntax of the command is incorrect.
我该怎么做?
编辑: 基于 Magoo 和 Batch / Find And Edit Lines in TXT file 的 RobW - 我的问题解决如下:
for /f "tokens=*" %%i in ('"FINDSTR /B Root: test.txt"') do set root=%%i
::root holds test.txt's line starts with "Root:"
echo %root%
SETLOCAL=ENABLEDELAYEDEXPANSION
::iterate on test.txt's lines and compare to the root's value
rename test.txt test.tmp
for /f "tokens=*" %%a in (test.tmp) do (
set foo=%%a
echo !foo!
echo %root%
echo "%root%"
if "!foo!"=="%root%" (set foo=hello)
echo !foo! >> test.txt)
del test.tmp
谢谢! 罗尼
【问题讨论】:
-
语法错误是因为 ( 必须与 DO 在同一行。
-
在您的第一个
for /F循环中,您需要提供选项"delims="以获取整行;否则,delims默认为 tab 和 space,因此只返回第一个标记...
标签: batch-file text replace