我有大约1000个文本文件,每个文件350 KB。文件命名为part0001到part1000。所有这些都存在于一个目录中。我需要编写一个批处理脚本来组合50个文件来生成一个文件。我知道我们可以使用'type'命令来连接文件和'for'命令来迭代文件,但我无法找到合适的解决方案。任何帮助将受到高度赞赏。
我想要类似的东西:
combine <number_files_to_cimbine_into_one> <soure_directory> <output_directory>
答案 0 :(得分:2)
这会将.txt文件收集到10个组中。它实际上并不附加它们;它显示一条消息,说明需要追加的地方。
@echo off
setlocal EnableDelayedExpansion
REM outcount is the current output file
set outcount=1
REM groupcount is the number of files seen in the current group
set groupcount=0
for %%I in (*.txt) do (
if !groupcount!==10 (
set /A outcount=!outcount!+1
set groupcount=0
)
echo Append %%I to combinedFile!outcount!
set /A groupcount=!groupcount!+1
)
答案 1 :(得分:1)
这应该对你有用,就像你要求的那样需要3个args,并将内容转储到dest目录中的out.txt。
@echo off
setlocal enabledelayedexpansion
set TOTALFILECOUNT=%1
set FROMDIR=%2
set TODIR=%3
set OUTFILENAME="%TODIR%\out.txt"
set COUNTER=0
FOR /f "tokens=*" %%A IN ( 'DIR /B "%FROMDIR%\*.txt"' ) DO (
SET /a COUNTER+=1
type %FROMDIR%\%%A >> %OUTFILENAME%
if !COUNTER!==%TOTALFILECOUNT% GOTO :EOF
)
endlocal