使用批处理文件从文件中删除最后 n 行 [英] Deleting last n lines from file using batch file

查看:45
本文介绍了使用批处理文件从文件中删除最后 n 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用批处理脚本从文件中删除最后 n 行

How to delete last n lines from file using batch script

我对批处理文件一无所知,我是第一次写批处理文件.

I don't have any idea about batch files, I am writing batch file for the first time.

我应该如何编写这个批处理文件?

How should I write this batch file?

对于 Windows7

For Windows7

试试看

<Project_Name>

    <Noter>
        <Common>
        <File>D:Project_NameUtil.jar</File>
        <File>D:Project_NameNoter.bat</File>
        <File>D:Project_NameNoter.xml</File>
        <File>D:Project_NameNoter.jar</File>       
        </Common>

        <Project_Name>
            <File>D:Util.bat</File>
            <File>D:Util.xml</File>
            <File>D:log.bat</File>
        </Project_Name>
    </Noter>
    <CCNET>

推荐答案

这是删除最后N行的完整脚本

This the complete script for remove last N line

  • 计算总行数
  • 设置 Line = Line - N ,只保留处理行号
@echo OFF
setlocal EnableDelayedExpansion

set LINES=0
for /f "delims==" %%I in (infile.txt) do (
    set /a LINES=LINES+1    
)

echo Total Lines : %LINES%
echo.

:: n = 5 , last 5 line will ignore 
set /a LINES=LINES-5

call:PrintFirstNLine > output.txt

goto EOF

:PrintFirstNLine
set cur=0
for /f "delims==" %%I in (infile.txt) do (      
    echo %%I        
    ::echo !cur! : %%I      
    set /a cur=cur+1    
    if "!cur!"=="%LINES%" goto EOF
) 

:EOF 

exit /b

这里调用:PrintFirstNLine >output.txt 将输出以外部文件名作为 output.txt

Here call:PrintFirstNLine > output.txt will give the output in an external file name as output.txt

输出样本输入

<Project_Name>      
<CBA_Notifier>      
    <Common>        
    <File>D:CBACBA_NotifierProject_NameIPS-Util.jar</File>      
    <File>D:CBACBA_NotifierProject_NameNotifier.bat</File>      
    <File>D:CBACBA_NotifierProject_NameNotifier.xml</File>      
    <File>D:CBACBA_NotifierProject_NameNotifier.jar</File>              
    </Common>       
    <Project_Name>      
        <File>D:CBACBA_NotifierIPS-Util.bat</File>   

删除最后 5 行

更新

:PrintFirstNLine
set cur=0
for /F "tokens=1* delims=]" %%I in ('type "infile.txt" ^| find /V /N ""') do (
   if "%%J"=="" (echo.) else (
        echo.%%J
        set /a cur=cur+1    
        )  

   if "!cur!"=="%LINES%" goto EOF
)

这篇关于使用批处理文件从文件中删除最后 n 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆