简体   繁体   English

如何通过批处理删除文本文件的重复部分?

[英]How can I remove recurring sections of a text file through batch?

So currently I have to run a CLI command to generate data of all the playlists in a folder, and the output text file is something like below.所以目前我必须运行 CLI 命令来生成文件夹中所有播放列表的数据,output 文本文件如下所示。

********************
PLAYLIST: 1
********************

<--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
......

<---- END FORUMS PASTE ---->

QUICK SUMMARY:

********************
PLAYLIST: 2
********************

<--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
.....

<---- END FORUMS PASTE ---->

QUICK SUMMARY:

********************
PLAYLIST: 3
********************

<--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
.....

<---- END FORUMS PASTE ---->

QUICK SUMMARY:
Required Data

From the little knowledge that I have, batch file can't remove it on the original file, but can instead create a new file with the processed data and that is fine for me.据我所知,批处理文件无法在原始文件上删除它,而是可以用处理后的数据创建一个新文件,这对我来说很好。

Now to what I am trying to achieve is that there are recurring sections for all the playlists in the output text file.现在我想要实现的是 output 文本文件中的所有播放列表都有重复的部分。 These are the sections beginning with <--- BEGIN FORUMS PASTE ---> and ending with <---- END FORUMS PASTE ----> .这些是以<--- BEGIN FORUMS PASTE --->开头并以<---- END FORUMS PASTE ---->结尾的部分。 So I am trying to remove every section that begins and ends with them, basically leaving out all the data that is not enclosed within that section.所以我试图删除以它们开头和结尾的每个部分,基本上省略了所有未包含在该部分中的数据。

I am not sure how to go about it, but I feel like the findstr command will come into use here or maybe a VBscript.我不确定如何 go 关于它,但我觉得findstr命令将在这里使用,或者可能是 VBscript。

The task can be done with the following batch file:该任务可以使用以下批处理文件完成:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Remove all environment variables defined by default for faster lines
rem processing with exception of the environment variable ComSpec, Path,
rem PATHEXT and SystemRoot. The last one is really used below.
for /F "delims==" %%I in ('set ^| %SystemRoot%\System32\findstr.exe /B /I /L /V "Comspec Path PATHEXT SystemRoot"') do set "%%I="

set "SourceFile=PlayList.txt"
if not exist "%SourceFile%" (
    echo ERROR: File not found: "%SourceFile%"
    echo/
    pause
    goto EndBatch
)

set "EmptyLine="
set "IgnoreLines="
set "FileModified="
set "TempFile=%SourceFile%.tmp"

(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%SourceFile%" 2^>nul') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    if not defined IgnoreLines (
        if "!Line:<--- BEGIN FORUMS PASTE --->=!" == "!Line!" (
            if "!Line:*:=!" == "" (
                if not "!EmptyLine!" == "2" echo(
                endlocal
                set "EmptyLine=1"
            ) else (
                echo(!Line:*:=!
                endlocal
                set "EmptyLine="
            )
        ) else (
            endlocal
            set "IgnoreLines=1"
            set "FileModified=1"
        )
    ) else (
        if "!Line:<---- END FORUMS PASTE ---->=!" == "!Line!" (
            endlocal
        ) else (
            endlocal
            set "IgnoreLines="
            if defined EmptyLine set "EmptyLine=2"
        )
    )
))>"%TempFile%"

if defined FileModified move /Y "%TempFile%" "%SourceFile%" >nul
if exist "%TempFile%" del "%TempFile%"

:EndBatch
rem Restore the initial execution environment with the initial variables list.
endlocal

Please read my answer on How to read and print contents of text file line by line?请阅读我关于如何逐行读取和打印文本文件内容的答案? It explains the reason why using FINDSTR and why using such a difficult code to process the lines of a text file with FOR of Windows command processor cmd.exe not designed for text file modifications at all.它解释了使用FINDSTR的原因以及为什么使用 Windows 命令处理器cmd.exeFOR来处理文本文件的行的原因,这根本不是为文本文件修改而设计的。

The batch file is designed to ignore also the first empty line below a line containing <---- END FORUMS PASTE ----> if there is an empty line above a line containing <--- BEGIN FORUMS PASTE ---> to avoid having finally two empty lines in the file on a removed block.如果在包含<--- BEGIN FORUMS PASTE --->的行上方有空行,则批处理文件也被设计为忽略包含<---- END FORUMS PASTE ---->的行下方的第一个空行以避免在已删除块上的文件中最后有两个空行。

Example: The source file contains the lines:示例:源文件包含以下行:

********************
PLAYLIST: 1
********************
    <--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
......

    <---- END FORUMS PASTE ---->

QUICK SUMMARY:

********************
PLAYLIST: 2
********************
BEGINNING LIST 2 <--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
.....

<---- END FORUMS PASTE ----> END LIST 2
QUICK SUMMARY:

********************
PLAYLIST: 3
********************

<--- BEGIN FORUMS PASTE --->

Some unnecessary Data
Some unnecessary Data
Some unnecessary Data
.....

<---- END FORUMS PASTE ---->

QUICK SUMMARY:
Required Data

This source file is modified by the batch file to:此源文件由批处理文件修改为:

********************
PLAYLIST: 1
********************

QUICK SUMMARY:

********************
PLAYLIST: 2
********************
QUICK SUMMARY:

********************
PLAYLIST: 3
********************

QUICK SUMMARY:
Required Data

Leading or trailing spaces/tabs or other characters around <--- BEGIN FORUMS PASTE ---> and <---- END FORUMS PASTE ----> do not matter for the identification of the beginning and the end of a block to remove from source file. <--- BEGIN FORUMS PASTE ---><---- END FORUMS PASTE ---->周围的前导或尾随空格/制表符或其他字符对于识别块的开头和结尾无关紧要从源文件中删除。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符window,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文本文件的内容加载到批处理文件变量中? - How can I load the contents of a text file into a batch file variable? 如何将文本文件的最后一行复制到另一个文本文件-批处理文件 - How can I copy the last line in a text file to another text file - batch file 如何制作一个批处理文件,告诉我文本文件的哪些行不在另一个文件中? - How can I make a batch file that will tell me which lines of a text file are NOT in another file? 给定一个包含cmd命令的文本文件,我如何使用批处理文件循环并运行它们? - Given a text file containing cmd commands, how can I use a batch file to loop and run them? 如何创建从批处理文件复制ping信息的文本文件? - How can I create a text file that copies ping info from the batch file? 如何静默运行批处理文件? - How can I run a batch file silently? 如何使用批处理脚本从文本文件创建用户并分配组? - How can I create users and assign group from a text file with a batch script? 如何将变量添加到批处理文件? - How can I add a variable to a batch file? 如何在批处理文件中回显换行符? - How can I echo a newline in a batch file? 如何从批处理文件中将文本文件中的字符串转换为数组? - How can I get string from text file to an array in batch files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM