我正在使用 Windows 命令提示符命令编写 BATCH 脚本,其中我需要将两个文本文件合并到一个源文件中: 输入文件:
ADAT_DZ01_20220320_104155.txt
AEDAT_DZ01_20220320_104155.txt
输出文件:
ADAT_DZ01_20220320_104155.txt
(合并两个输入文件的结果)
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句