Устанавливаем дату создания файла

Материал из DRKB

Устанавливаем дату создания файла[править | править код]

function SetFileDate(const FileName: string; const FileDate: TDateTime): Boolean;
var
  FileHandle: THandle;
  FileSetDateResult: Integer;
begin
  try
    try
      FileHandle := FileOpen(FileName, fmOpenWrite OR fmShareDenyNone);
      if FileHandle > 0 then
      begin
        FileSetDateResult := FileSetDate(FileHandle, DateTimeToFileDate(FileDate));
        Result := (FileSetDateResult = 0);
      end;
    except
      Result := False;
    end;
  finally
    FileClose(FileHandle);
  end;
end;

{ Использование: }

SetFileDate('c:\mydir\myfile.ext', Now)


Source: http://delphiworld.narod.ru/
ID: 03150



var
  f: file;
begin
  Assign(f, DirInfo.Name);
  Reset(f);
  SetFTime(f, Time);
  Close(f);
end;


Source: http://delphiworld.narod.ru/
ID: 03151



Функция, которая устанавливает дату одного файла, равную дате другого файла


procedure CopyFileDate(const Source, Dest: String);
var
  SourceHand, DestHand: word;
begin
  SourceHand := FileOpen(Source, fmOutput);       { открываем исходный файл }
  DestHand := FileOpen(Dest, fmInput);            { открываем целевой файл }
  FileSetDate(DestHand, FileGetDate(SourceHand)); { получаем/устанавливаем дату }
  FileClose(SourceHand);                          { закрываем исходный файл }
  FileClose(DestHand);                            { закрываем целевой файл }
end;


Source: http://delphiworld.narod.ru/
ID: 03152