
我想使用以下方法將檔案「scholar (1).txt」複製到「scholar (25).txt」到單一 .txt 檔案: wikihow:在命令提示字元中合併文字 (.Txt) 文件
for %f in (*.txt) do type "%f" >> output.txt
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
這會在環境中建立一個數組,使我們能夠檢索正確的數字排序的檔案清單。