把文本文件1.txt第二行替换为“AAAAAA“并删除第五行以后的所有内容
2024-10-24 乱云飞
@echo off
setlocal enabledelayedexpansion
:: 定义变量
set "inputFile=1.txt"
set "outputFile=temp.txt"
set "replacement=AAAAAA"
set "lineCounter=0"
set "shouldWrite=1"
:: 创建并清空临时文件
> "%outputFile%" type nul
:: 逐行处理输入文件
for /f "usebackq delims=" %%A in ("%inputFile%") do (
set /a lineCounter+=1
if !lineCounter! equ 2 (
set "currentLine=%replacement%"
) else (
set "currentLine=%%A"
if !lineCounter! geq 5 (
set "shouldWrite=0"
)
)
if !shouldWrite! equ 1 (
echo(!currentLine!>>"%outputFile%"
)
)
:: 用临时文件覆盖原文件
move /y "%outputFile%" "%inputFile%" >nul
:: 清理环境变量(在这个脚本中不是必需的)
endlocal
echo 完成
pause
本文链接:http://80c.cc/ez/781.html
发表评论: