使用cmd按数字顺序将多个文本文件复制到一个文本文件

使用cmd按数字顺序将多个文本文件复制到一个文本文件

我想将文件“scholar (1).txt”复制到“scholar (25).txt”到单个 .txt 文件,使用以下方法: wikihow:在命令提示符中合并文本(.Txt)文件

for %f in (*.txt) do type "%f" >> output.txt

SU:有没有办法使用 Windows 命令行合并文本文件?

copy *.txt bigfile.txt

type *.js > all.txt

但所有文件并不是按顺序复制的:

`C:\..\Documents\New folder>type *.txt >all.txt

 scholar (1).txt
 scholar (10).txt
 scholar (11).txt
 scholar (12).txt
 scholar (13).txt
 scholar (14).txt
 scholar (15).txt
 scholar (16).txt
 scholar (17).txt
 scholar (18).txt
 scholar (19).txt
 scholar (2).txt
 scholar (20).txt
 scholar (21).txt
 scholar (22).txt
 scholar (23).txt
 scholar (24).txt
 scholar (25).txt
 scholar (3).txt
 scholar (4).txt
 scholar (5).txt
 scholar (6).txt
 scholar (7).txt
 scholar (8).txt
 scholar (9).txt`

答案1

(for /l %a in (1 1 25) do @type "scholar (%a).txt")>all.txt

为了在批处理文件中使用,百分号需要加倍,替换%%%

如果文件总数未知,但文件名称如所示,则可以使用此代码

@echo off
    setlocal enableextensions disabledelayedexpansion

    for %%z in ("scholar (*).txt") do for /f "tokens=2 delims=()" %%a in ("%%~nxz") do (
        set /a "num=1000000000+%%a"
        setlocal enabledelayedexpansion
        for %%b in (!num!) do endlocal & set "f[%%b]=%%~fz"
    )
    (for /f "tokens=1,* delims==" %%a in ('2>nul set f[') do type "%%b") > all.txt

这会在环境中创建一个数组,使我们能够检索正确按数字排序的文件列表。

相关内容