Cmd合并.txt文件并删除行

问题描述 投票:-1回答:1

我有3个这样的Txt格式的文件:

a.txt
b.txt
c.txt

我使用cmd合并了3个这样的文件:

For %I In ("%CD%")Do @Copy *.* "%~nxI.txt"

现在文件已成功合并为一个,但是我希望在合并之前删除所有行,直到LINE name

交易结束

或。

TRANSACTION start

谢谢

batch-file cmd
1个回答
0
投票

以下注释的批处理文件代码可能用于此任务。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Use name of current folder as output file name.
for %%I in (.) do set "OutputFile=%%~nxI.txt"

rem Is the current folder not root folder of a drive?
if not "%OutputFile%" == ".txt" goto DeleteOutputFile

rem Use for root folder of a drive a generic file name containing drive letter.
for %%I in (.) do set "OutputFile=%%~dI"
set "OutputFile=RootDrive%OutputFile:~0,1%.txt"

:DeleteOutputFile
del "%OutputFile%" 2>nul

rem Get a list of *.txt files in current directory into memory of Windows
rem command processor not containing the output file which will have also
rem the file extension .txt and will be stored also in current directory.
rem Then process this list of text files with ignoring all lines up to
rem first line containing case-insensitive either TRANSACTION END or
rem TRANSACTION START. The code below is written to really output every
rem line in the text files below the line with one of the identifier
rem strings including empty lines and lines starting with a semicolon.

for /F delims^=^ eol^= %%I in ('dir *.txt /A-D /B /ON 2^>nul') do (
    set OutputLines=
    for /F delims^=^ eol^= %%J in ('%SystemRoot%\System32\findstr.exe /N "^" "%%I" 2^>nul') do (
        set "Line=%%J"
        setlocal EnableDelayedExpansion
        if defined OutputLines (
            echo(!Line:*:=!
            endlocal
        ) else (
            if not "!Line:TRANSACTION END=!" == "!Line!" (
                endlocal
                set "OutputLines=1"
            ) else if not "!Line:TRANSACTION START=!" == "!Line!" (
                endlocal
                set "OutputLines=1"
            ) else endlocal
        )
    )>>"%OutputFile%"
)

rem Delete the output file if being an empty file because of text files
rem found in current directory, but none of them contain a line with one
rem of the two identifier strings.

if exist "%OutputFile%" for %%I in ("%OutputFile%") do if %%~zI == 0 del "%OutputFile%"
endlocal

使用Windows命令处理器cmd.exe处理的批处理文件执行此文本文件合并任务,绝对是此任务的最差选择。 cmd.exe设计用于执行命令和可执行文件,但不适用于文件内容处理任务。因此,必须使用特殊代码才能真正处理所有行的ANSI或UTF-8编码的文本文件,如我在How to read and print contents of text file line by line?的答案中所详细描述的那样,与使用以下两种方法之一的其他解决方案相比,该特殊代码使文件处理极其缓慢用C / C ++ / C#或其他编程语言编写的应用程序,已编译为可执行文件或其他脚本解释器(例如Windows Script HostPowerShell),默认情况下已安装在Windows上,或其他脚本解释器(例如Python或Perl)。

为了了解所使用的命令及其工作方式,请打开command prompt窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

也请阅读有关Using command redirection operators的Microsoft文章,以获取2>nul的解释。当Windows命令解释器在执行命令FOR之前处理此命令行时,重定向操作符>必须在两个FOR命令行上都使用插入符号^进行转义,并使用2>nul解释为文字字符。在后台以dir和附加两个findstr之间的命令行开始的单独命令过程中,分别执行嵌入的%ComSpec% /c命令行。

© www.soinside.com 2019 - 2024. All rights reserved.