下面这段例子是我写的文件合并器用的,你看看,来不及改了。
var
FS: TFileStream;
MS: TMemoryStream;
i, FileCount: integer;
FileName, SaveFile: string;
FileList: TStrings;
begin
AFile := ReplaceAll(AFile, '"', '');
FileName := ExtractFileName(AFile);
SaveFile := ChangeFileExt(FileName, '');
FileName := ChangeFileExt(FileName, '.*');
FileList := TStringList.Create;
if SearchFile(ExtractFilePath(AFile), FileName, FileList) then
begin
FileCount := 0;
for i := 0 to FileList.Count - 1 do
begin
try
if (StrToInt(ExtName(FileList.Strings)) > FileCount) then
FileCount := StrToInt(ExtName(FileList.Strings));
except
Application.MessageBox('分割文件有误!', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
exit;
end;
end;
try
if GetStrRight(deSave.Text, 1) <> '/' then
FS := TFileStream.Create(deSave.Text + '/' + SaveFile, fmCreate)
else
FS := TFileStream.Create(deSave.Text + SaveFile, fmCreate)
except
Application.MessageBox('创建文件错误,请确认文件是否已经存在。', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
exit;
end;
for i := 1 to FileCount do
begin
MS := TMemoryStream.Create;
try
MS.LoadFromFile(ExtractFilePath(AFile) + SaveFile + CreateExtName(i));
except
Application.MessageBox('读取文件错误,请确认分割文件是否完整。', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
MS.Free;
FS.Free;
exit;
end;
FS.CopyFrom(MS, MS.Size);
MS.Free;
end;
FS.Free;
end;
FileList.Free;
Application.MessageBox('合并文件成功!', '信息', MB_OK + MB_ICONINFORMATION);
end;