在批处理脚本中使用命令行 (windows) 连接两个文本文件

Concatenate two text files using command line (windows) in a Batch script

我正在使用 windows 命令提示符命令编写 BATCH 脚本,我需要在其中一个源文件中合并两个文本文件: 输入文件:

ADAT_DZ01_20220320_104155.txt
AEDAT_DZ01_20220320_104155.txt                          

输出文件:

ADAT_DZ01_20220320_104155.txt 

(合并 2 个输入文件的结果)

PS/ 每次从应用程序中提取时,文件名都会更改:

ADAT_DZ01_[the changing part].txt
AEDAT_DZ01_[the changing part].txt

如果你能帮助我,我将不胜感激。

copy /b ADAT*.txt + AEDAT*.txt AEDAT*.txt 没用。
more ADAT*.txt AEDAT*.txt >> ADAT*.txt 也没用

这批应该可以解决问题,请随意调整。 请仔细阅读源内的评论。

@echo off
setlocal enableextensions enabledelayedexpansion

REM Defining base prefix for both files.
set F1_PREFIX=ADAT_DZ01_
set F2_PREFIX=AEDAT_DZ01_

pushd %~dp0
REM Searching all "F1" files. No need to search for "F2".
for %%A in (%F1_PREFIX%*.txt) do (
    echo Found file: %%A
    REM Splitting the file name to get the variable part.
    for /f "tokens=2,* delims=_" %%B in ("%%A") do (
        echo    Found suffix: %%C
        REM Build second file name.
        set second=!F2_PREFIX!%%C
        REM Second file exist? Need to merge.
        if exist !second! (
            echo    Merging %%A and !second! into %%A...
            REM Simply append content of 2nd file at end of 1st file.
            type !second!>>%%A
            REM Remove 2nd file, useless now, and we don't want to append it twice.
            del /s /q !second! > NUL 2>&1
        ) else (
            REM 2nd file does not exist? Already merged.
            echo    Already merged, skipping...
        )
        echo    Done.
    )
)
popd
goto :eof

嗯......据我了解,你想追加每个AEDAT_...文件到相应的ADAT_...文件的末尾(你是不清楚“合并两个文件”是什么意思)。如果是这样,这是一个解决方案:

@echo off
for /F "tokens=1* delims=_" %%a in ('dir /B AEDAT_*.txt') do (
   (echo/
    type "%%a_%%b") >> "ADAT_%%b"
   del "%%a_%%b"
)