unit MyUtils;
interface
uses SysUtils, ComCtrls, Dialogs;
// SourceFileName, TargetFileName - vollständige Dateinamen
// Zielpfad wird erzeugt falls nicht vorhanden
function CopyFileWithProgress(SourceFileName, TargetFileName:
String; aPB:TProgressBar):Integer;
procedure CreatePath(Path:
String);
function ChkPath(Path:
String):
String;
implementation
function CopyFileWithProgress(SourceFileName, TargetFileName:
String; aPB:TProgressBar):Integer;
const
BufSize:Integer=10240;
var
Path:
String;
fi,fo:
File;
fSize,nRead,nWrite:Integer;
Buf:Pointer;
begin
Result:=0;
Path:=ExtractFilePath(TargetFileName);
try
ChkPath(Path);
except
MessageDlg('
Error creating Path: '+Path, mtError, [mbCancel], 0);
exit;
end;
try AssignFile(fi, SourceFileName); Reset (fi,1)
except exit
end;
try AssignFile(fo, TargetFileName); Rewrite(fo,1)
except CloseFile(fi); exit
end;
fSize:=FileSize(fi);
if Assigned(aPB)
then
begin
aPB.Position:=0;
aPB.Max:=fSize;
end;
try
GetMem(Buf ,BufSize)
except
CloseFile(fi);
CloseFile(fo);
exit
end;
try
repeat
BlockRead (fi,Buf^,BufSize, nRead);
BlockWrite(fo,Buf^,nRead, nWrite);
if Assigned(aPB)
then aPB.Position:=FilePos(fi);
until nWrite<BufSize;
FileSetDate(TFileRec(fo).Handle, FileGetDate(TFileRec(fi).Handle));
finally
FreeMem(Buf);
CloseFile(fi);
CloseFile(fo);
if Assigned(aPB)
then aPB.Position:=0;
Result:=fSize;
end;
end;
procedure CreatePath;
var
si:Integer;
begin
if Path='
'
then exit;
if Path[Length(Path)]<>'
\'
then Path:=Path+'
\';
for si:= 4
to Length(Path)
do
if Path[si]='
\'
then CreateDir(Copy(Path,1,si-1));
end;
function ChkPath(Path:
String):
String;
begin
Result:=Path;
CreatePath(Path);
end;
end.