Batch file to delete first 3 lines of a text file

后端 未结 5 1795
广开言路
广开言路 2020-11-28 14:06

As the title states I need a batch file to delete the FIRST 3 lines of a text file.

for example:

A    
B    
C    
D    
E   
F    
G
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 14:36

    This should do it

    for /f "skip=3 delims=*" %%a in (C:\file.txt) do (
    echo %%a >>C:\newfile.txt    
    )
    xcopy C:\newfile.txt C:\file.txt /y
    del C:\newfile.txt /f /q
    

    That will re-create the file with the first 3 lines removed.

    To keep the user updated you could integrate messages in the batch file in vbscript style or output messages in the command prompt.

    @echo off
    echo Removing...
    for /f "skip=3 delims=*" %%a in (C:\file.txt) do (
    echo %%a >>C:\newfile.txt
    ) >nul
    echo Lines removed, rebuilding file...
    xcopy C:\newfile.txt C:\file.txt /y >nul
    echo File rebuilt, removing temporary files
    del C:\newfile.txt /f /q >nul
    msg * Done!
    exit >nul
    

    Hope this helps.

提交回复
热议问题