Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get DateTime of a File 1

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi

I'd like to get the datetime of a given filename. I've tried this so far but keeps returning INVALID_HANDLE_VALUE:

Code:
procedure TForm1.Button1Click(Sender: TObject);
  var WriteTime    : TFileTime;
      FileDateTime : TDateTime;
      hfile        : Thandle;
      Security     : TSecurityAttributes;
      pInputFile   : array[0..MAX_PATH] of Char;
begin
  mmResults.clear;

  StrPCopy(pInputFile, edfile.text);

  hFile := Createfile(
         pInputFile,
         GENERIC_READ,
         FILE_SHARE_READ,// 0,
         @Security,
         OPEN_EXISTING,
         FILE_ATTRIBUTE_NORMAL,
         0);


  if hfile = INVALID_HANDLE_VALUE then
  begin
    mmResults.lines.add('File not found - Invalid Handle Value');
  end
  else
  begin
    GetfileTime(hFile, nil, nil, @WriteTime);
    FileDateTime := FileTime2DateTime(WriteTime);
    mmResults.lines.add(FormatDateTime('dd mmm yyyy hh:nn:ss.mss', FileDatetime));
  end;
end;

function TForm1.FileTimeToDAteTime(FileTime: TFileTime): TDateTime;
var
  LocalFileTime: TFileTime;
  SystemTime: TSystemTime;
begin
  try
    FileTimeToLocalFileTime(FileTime, LocalFileTime);
    FileTimeToSystemTime(LocalFileTime, SystemTime);
    Result := SystemTimeToDateTime(SystemTime);
  except
  on E: Exception do
    showmessage('FileTimeToDAteTime : '+e.message + ' datetime - ' +datetimetostr(Result));
  end;
end;

I just wish to get the datetime of the file, I don't want to change any properties of the file.

Thanks in advance for help.
Many thanks,
Lou
 
have you looked at the TSearchRec component?

It allows you to "find" a file and then extract certain attributes:


snippet:
Code:
var
sr : TSearchRec;
begin
FindFirst(ExtractFilePath(Application.ExeName) + '\Scanned\*.tif', faAnyFile, sr);
  repeat
    if ProcessScannedFile(ExtractFilePath(Application.ExeName) + '\Scanned\' + sr.Name) then
      DeleteFile(ExtractFilePath(Application.ExeName) + '\Scanned\' + sr.Name);
  until FindNext(sr) <> 0;
  FindClose(sr);

this process looks in the directory the application is run from (ExtractFilePath) in the Scanned folder for any files with tif extension and assigns that file to the variable sr. I then use sr.Name to get the name of the file, but one of the other attributes is Time, which according to the help files:
Time contains the time stamp of the file. It can be converted to a TDateTime value using FileDateToDateTime.

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases:
The Fundamentals of Relational Database Design
Understanding SQL Joi
 
If you are not filling the SecAttributes record use NIL in the function call.

buho (A).
 
hi LesPaul

Thanks for the reply, I'm aware of the searching but I don't want to waste time going through files I don't need as I know the specific file I want to look at.

Buho, I'll try your suggestion and let you know.

Thanks guys
Lou
 
Lou I'm not sure why you need this long-winded approach. Sorry if I've misunderstood, but:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  dt : TDatetime;
begin
  dt := FileDateToDateTime(FileAge('c:\zzz.txt'));
//Test the result:
  ShowMessage(DateTimeToStr(dt))
end;

seems a much simpler approach, given that you already know the filename.


Hope this helps.

[vampire][bat]
 
earthandfire, you're a genius, thanks, mate!

That works perfectly. I didn't know of the FileAge function.

Thanks again.

www.radbmx.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top