简体   繁体   English

使用Windows Batch替换文本文件中的一行

[英]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.

相关问题 查找直到该行,然后用另一个文本文件Windows批处理脚本替换 - Find until that line and replace with another text file windows batch script Windows Batch - 查找一行并替换为文本 - Windows Batch - Find a line and replace with text 如何使用Windows批处理脚本在“,”中查找和替换文本文件中的“=”? - How to find and replace “=” in a text file with “,” using windows batch script? 使用IF-批处理文件将一行替换为另一行 - Replace a line with another line using IF - Batch file 通过 Windows 批处理文件用文本文件中的 unicode 替换字符串 - Replace string with unicode in text file via Windows batch file 使用批处理脚本在文本文件中查找和替换字符串的算法,有效,但随机停止 - Find and replace algorithm for string in text file using batch script, works, but stopping at random Line 如何使用 Windows 批处理文件遍历文本文件中的每一行? - How do you loop through each line in a text file using a windows batch file? 批处理文件以添加或替换文本文件中的单词/行 - batch files to add or replace a word/line in a text file 使用 Windows 批处理提取文本文件的一部分 - Extract part of a text file using Windows batch 如何从批处理文件中将行号添加到文本文件中(Windows) - How to add line numbers to a text file from a batch file (Windows)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM