@echo off setlocal EnableDelayedExpansion set "file=path/to/file.txt" set "tempfile=%file%.temp" set "linenumber=10" rem read the file and write the modified content to a temporary file set "count=0" (for /f "usebackq delims=" %%i in ("%file%") do ( set /a "count+=1" if !count! neq %linenumber% echo %%i )) > "%tempfile%" rem overwrite the original file with the modified content move /y "%tempfile%" "%file%" > nul 添加一个替换指定多少行文本内容功能
时间: 2023-07-13 12:23:02 浏览: 46
好的,以下是一个示例批处理脚本,用于替换指定路径下的notepad文本文件中的指定行的内容,并将修改后的内容保存到原文件中。请将其中的"path/to/file.txt"替换为您需要处理的文件路径,将"10"替换为您需要替换内容的行数,将"old text"替换为需要替换的旧文本内容,将"new text"替换为需要替换成的新文本内容。
```
@echo off
setlocal EnableDelayedExpansion
set "file=path/to/file.txt"
set "tempfile=%file%.temp"
set "linenumber=10"
set "oldtext=old text"
set "newtext=new text"
rem read the file and write the modified content to a temporary file
set "count=0"
(for /f "usebackq delims=" %%i in ("%file%") do (
set /a "count+=1"
if !count! neq %linenumber% (
echo %%i
) else (
echo %%i | findstr /v /c:"%oldtext%" || (
set "line=%%i"
set "line=!line:%oldtext%=%newtext%!"
echo !line!
)
)
)) > "%tempfile%"
rem overwrite the original file with the modified content
move /y "%tempfile%" "%file%" > nul
```
注意:请谨慎使用此脚本,尤其是对于重要的文件。建议先备份原文件,以免数据丢失。