[英]Replace a line in a text file using windows batch
I have text file that has the following contents 我的文本文件包含以下内容
status=y
I need to change it to 我需要将其更改为
status=n
using a windows batch script 使用Windows批处理脚本
I tried this but I get a syntax error 我尝试了这个,但是出现语法错误
setlocal enabledelayedexpansion
set INTEXTFILE=status.txt
set OUTTEXTFILE=t.txt
set SEARCHTEXT='status=y'
set REPLACETEXT='status=n'
set OUTPUTLINE=
for /f "tokens=1,* delims=" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
Test this on a sample file. 在样本文件上对此进行测试。
@echo off
type status.txt|repl "status=y" "status=n" L >temp.tmp
move temp.tmp status.txt >nul
The above uses a helper batch file called repl.bat
- download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat 上面使用了一个名为repl.bat
的帮助程序批处理文件-从以下网址下载: https : repl.bat
Place repl.bat
in the same folder as the batch file or in a folder that is on the path. 将repl.bat
放在与批处理文件相同的文件夹中或路径上的文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.