Delphi -- File Yönetimi İpuçları
Delphi -- File Yönetimi İpuçları
1. Delphi 'de dosya nasıl Kopyalanır ?
Bunun için bir kaç yöntem var...
Procedure FileCopy( Const sourcefilename, targetfilename: String );
Var
S, T: TFileStream;
Begin
S := TFileStream.Create( sourcefilename, fmOpenRead );
try
T := TFileStream.Create( targetfilename,
fmOpenWrite or fmCreate );
try
T.CopyFrom(S, S.Size ) ;
finally
T.Free;
end;
finally
S.Free;
end;
End;
procedure FileCopy(const FromFile, ToFile: string);
var
FromF, ToF: file;
NumRead, NumWritten: Word;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, FromFile);
Reset(FromF, 1);
AssignFile(ToF, ToFile);
Rewrite(ToF, 1);
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
end;
{ USES LZExpand.}
procedure CopyFile(FromFileName, ToFileName: string);
var
FromFile, ToFile: File;
begin
AssignFile(FromFile, FromFileName);
AssignFile(ToFile, ToFileName);
Reset(FromFile);
try
Rewrite(ToFile);
try
if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0 then
raise EInOutError.Create
finally
CloseFile(ToFile);
end;
finally
CloseFile(FromFile);
end;
end;
2. Dosyanın değiştirilme tarih ve saatinin öğrenilmesi
function GetModifiedDate(FileName: string): TDateTime;
var
SearchRec: TSearchRec;
ModifiedTime: TFileTime;
SystemTime: TSystemTime;
begin
Result := 0;
If FindFirst(FileName, faAnyFile, SearchRec) = 0 then
begin
ModifiedTime := SearchRec.FindData.ftLastWriteTime;
FileTimeToLocalFileTime(ModifiedTime, ModifiedTime);
FileTimeToSystemTime(ModifiedTime, SystemTime);
Result := SystemTimeToDateTime(SystemTime);
end;
FindClose(SearchRec);
end;
3. Dosyanın varlığının kontrol edilmesi
Function GetShortFileName(Const FileName : String) : String;
var
aTmp: array[0..255] of char;
begin
if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
Result:= FileName+ ' <--Bulunamadı'
else
Result:=FileName+' <--Bulundu';
end;
4. Verilen bir dizinin içini silme
procedure DelTree(StartDir: string);
var
Search : TSearchRec;
begin
if Startdir[Length(Startdir)] <> '\' then
startdir := startdir + '\';
if FindFirst(startdir + '*.*', faAnyFile, Search) = 0 then
repeat
if (Search.Name[1] <> '.' ) then
if ((Search.Attr and faDirectory) > 0) then
begin
RmDir(StartDir + Search.Name);
ChangeFAttrib(StartDir + Search.Name);
end else
begin
DeleteFile(StartDir + Search.Name);
Application.ProcessMessages;
end;
until FindNext(Search) <> 0;
FindClose(Search);
end
--------------------------------------------------------------------------------