如何獲得檔案的建立日期

有關Delphi 的語法, 程式, 等
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

如何獲得檔案的建立日期

文章 tim »

由於原來的 fileage 是 ftLastWriteTime (最後修改時間), 但不能查建立時間和最後存取時間, 參考了一下 delphi 的 FileAge Function 後, 改寫了一下功能, FileAgeEx function, 其中第一個參數是檔名, 第二個參數是要取得的時間是代碼
1. 建立時間 2. 最後存取時間 3. 最後修改時間
回傳為檔案時間格式, 若是 -1 則表示找不到該檔(如原來的 FileAge Function一樣)
然後再利用 FileDateToDateTime function 來將傳回的檔案時間轉為 TDateTime 的格式.
以下為範例:

代碼: 選擇全部

     
    procedure Tform1.Button1Click(Sender: TObject); 
    var 
      t0, t1, t2, t3: string; 
    begin 
      t0:=formatdatetime('yyyy/mm/dd hh:nn:ss',FileDateToDateTime(FileAge('c:AspiLog.TXT'))); 
      t1:=formatdatetime('yyyy/mm/dd hh:nn:ss'',FileDateToDateTime(FileAgeEx('c:AspiLog.TXT',1))); 
      t2:=formatdatetime('yyyy/mm/dd hh:nn:ss'',FileDateToDateTime(FileAgeEx('c:AspiLog.TXT',2))); 
      t3:=formatdatetime('yyyy/mm/dd hh:nn:ss'',FileDateToDateTime(FileAgeEx('c:AspiLog.TXT',3))); 
      Memo1.Lines.Add('fileage     : ' + t0); 
      Memo1.Lines.Add('建立時間    : ' + t1); 
      Memo1.Lines.Add('最後存取時間: ' + t2); 
      Memo1.Lines.Add('最後修改時間: ' + t3); 
    end; 
    //.... 
    // 
     
     
    function FileAgeEx(const FileName: string; types: Integer): Integer; 
    var 
      Handle: THandle; 
      FindData: TWin32FindData; 
      LocalFileTime: TFileTime; 
    begin 
      Handle := FindFirstFile(PChar(FileName), FindData); 
      if Handle <> INVALID_HANDLE_value then 
      begin 
        Windows.FindClose(Handle); 
        if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then 
        begin 
          case types of 
            1: // 建立時間 
              FileTimeToLocalFileTime(FindData.ftCreationTime, LocalFileTime); 
            2: // 最後存取時間 
              FileTimeToLocalFileTime(FindData.ftLastAccessTime, LocalFileTime); 
            3: // 最後修改時間 
              FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime); 
          end; 
          if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi, 
            LongRec(Result).Lo) then Exit; 
        end; 
      end; 
      Result := -1; 
    end; 
多多留言, 整理文章, 把經驗累積下來.....
回覆文章