Batch file 用于删除特定行号的批处理文件

Batch file 用于删除特定行号的批处理文件,batch-file,line,lines,Batch File,Line,Lines,我有一个文件,每24小时更新一次,新的数据应该添加到末尾,但是文件开头的一些数据变得无关紧要。我需要的是一个批处理文件,该文件将删除第3行和第4行,然后使用相同的名称保存该文件 例如,假设文件是file.txt,它看起来像这样: A. B C D E F 我需要删除第3行和第4行,因此文件现在看起来如下所示: A. B E F 非常感谢您的帮助 以下是通过删除第3行和第4行来修改文件的批处理代码 这是充分的评论。所以我希望你能理解 您需要在第五行中修改要修改的文件的路径和名称 @echo off

我有一个文件,每24小时更新一次,新的数据应该添加到末尾,但是文件开头的一些数据变得无关紧要。我需要的是一个批处理文件,该文件将删除第3行和第4行,然后使用相同的名称保存该文件

例如,假设文件是file.txt,它看起来像这样:

A. B C D E F 我需要删除第3行和第4行,因此文件现在看起来如下所示:

A. B E F
非常感谢您的帮助

以下是通过删除第3行和第4行来修改文件的批处理代码

这是充分的评论。所以我希望你能理解

您需要在第五行中修改要修改的文件的路径和名称

@echo off
setlocal EnableDelayedExpansion

rem Define name of file to modify and check existence.
set "FileToModify=C:\Temp\Test.tmp"
if not exist "%FileToModify%" goto EndBatch

rem Define name of temporary file and delete this file if it currently
rem exists for example because of a breaked previous batch execution.
set "TempFile=%TEMP%\FileUpdate.tmp"
if exist "%TempFile%" del "%TempFile%"

rem Define a line number environment variable for temporary usage.
set "Line=0"

rem Process the file to modify line by line whereby empty lines are
rem skipped by command FOR and all other lines are just copied to
rem the temporary file with the exception of line 3 and line 4.
for /F "useback delims=" %%L in ("%FileToModify%") do (
    if !Line! GTR 3 (
        echo %%L>>"%TempFile%"
    ) else (
        rem Increment line number up to number 4.
        set /A Line+=1
        rem Copy line 1 and 2, but not line 3 and 4.
        if !Line! LSS 3 echo %%L>>"%TempFile%"
    )
)

rem Copy the temporary file being a copy of file to modify with
rem the exception of removed line 3 and 4 over the file to modify.
rem Finally delete the temporary file.
copy /Y "%TempFile%" "%FileToModify%" >nul
del "%TempFile%"

:EndBatch
endlocal

你试过什么吗?发布一些代码。我没有,因为我不知道如何编写批处理文件。我知道这是一种填鸭式的要求,但我不知道从哪里开始,而且我的谷歌搜索没有一个是非常有用的。