文件切割子程序

//分解文件夹 SDir:源目录名 DDir:目的目录名 SQZ:生成的子目录(前)后缀名

//SExt:文件类型 MNum:每个文件夹的文件数目 B:SQZ为前缀还是后缀名

procedure CutDir(SDir,DDir,SQZ,SExt:string;MNum:integer;B:Boolean;handle:THandle);

var

i,iFileCount,iFileNum,iDirCount,iDirNum:integer;

tsr:TStringList;

DFileName,SFileName:string;

bSearch:Boolean;

begin

bSearch:=False;

tsr:=TStringList.Create;

tsr:=FkFileListGet(SExt,SDir,False);

if SDir[Length(SDir)]<>'\' then

SDir :=SDir+'\';

if DDir[Length(DDir)]<>'\' then

DDir :=DDir+'\';

for i:=0 to tsr.count-1 do

begin

SFileName:=tsr[i];

if not bSearch then

begin

iDirCount :=GetDirCount(SDir+'*.*');

iDirNum :=iDirCount;

end;

if iDirNum=0 then

begin

if B then

begin

MkDir(DDir+SQZ+'1');

DFileName:=DDir+SQZ+'1\'+ExtractFileName(SFileName);

end

else

begin

MkDir(DDir+'1'+SQZ);

DFileName:=DDir+'1'+SQZ+'\'+ExtractFileName(SFileName);

end;

iFileNum :=1;

end

else

begin

if not bSearch then

begin

if B then

iFileCount:=GetFileCount(DDir+SQZ+IntToStr(iDirCount) ,SExt)

else

iFileCount:=GetFileCount(DDir+SQZ+IntToStr(iDirCount) +SQZ,SExt)

iFileName :=iFileCount;

bSearch :=True;

end;

if iFileNum>=MNum then

begin

if B then

begin

MKDir(DDir+SQZ+IntToStr(iDirNum+1));

DFileName :=DDir+SQZ+IntToStr(iDirNum+1)+'\'+ExtractFileName(SFileName);

end

else

begin

MKDir(DDir+SQZ+IntToStr(iDirNum+1)+SQZ);

DFileName :=DDir+IntToStr(iDirNum+1)+SQZ+'\'+ExtractFileName(SFileName);

end;

iDirNum :=iDirNum+1;

iFileNum :=1;

end

else

begin

if B then

DFileName:=DDir+SQZ+IntToStr(iDirNum)+'\'+ExtractFileName(SFileName);

else

DFileName:=DDir+IntToStr(iDirNum)++SQZ+'\'+ExtractFileName(SFileName);

iFileNum:=iFileNum+1;

end;

end;

MoveFile(Handle,SFileName,DFileName);

end;

end;

//调用实例,将F:\djcproj\技术文章内的所有txt文件以每20个作为一个文件夹的形式,切割到各子目录中,且子目录形//式为S1,S2.....

CutDir('F:\djcproj\技术文章','F:\djcproc\技术文章','S','*.txt',20,True,Handle);

原文地址:https://www.cnblogs.com/djcsch2001/p/2035841.html