* * *

Author Topic: how to get the original filedate not date modifyed  (Read 4878 times)

ratmalwer

  • New member
  • *
  • Posts: 41
how to get the original filedate not date modifyed
« on: November 07, 2017, 01:13:10 pm »
Hello

I have this statement witch works

Code: Pascal  [Select]
  1. FileDateTime := FormatDateTime('YYYY.MM.DD hh:mm:ss', FileDateToDateTime(FileAge(myFilename)));
Result = 2015.08.14 20:15:48

When I check it with the Windows-Explorer I see that this result is the date-modified.
How can I get the original-date of the file (created by my camara) witch is 2006.04.04 12:28 ???

ASerge

  • Hero Member
  • *****
  • Posts: 859
Re: how to get the original filedate not date modifyed
« Reply #1 on: November 07, 2017, 04:48:18 pm »
Code: Pascal  [Select]
  1. uses Windows;
  2.  
  3. function TryGetFileTimeCreated(const FileName: string; out Created: TDateTime): Boolean;
  4. var
  5.   R: TSearchRec;
  6.   UTCTime, LocalTime: TSystemTime;
  7. begin
  8.   Result := FindFirst(FileName, faArchive, R) = 0;
  9.   if Result then
  10.   begin
  11.     SysUtils.FindClose(R);
  12.     Result := FileTimeToSystemTime(@R.FindData.ftCreationTime, @UTCTime) and
  13.       SystemTimeToTzSpecificLocalTime(nil, @UTCTime, @LocalTime);
  14.   end;
  15.   if Result then
  16.     Created := SystemTimeToDateTime(LocalTime)
  17.   else
  18.     Created := 0;
  19. end;
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. var
  23.   Created: TDateTime;
  24. begin
  25.   if TryGetFileTimeCreated(Application.ExeName, Created) then
  26.     Caption := DateTimeToStr(Created);
  27. end;

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #2 on: November 07, 2017, 06:08:05 pm »
Hi Serge

thanks for this clear funtion.... it works nicely but does not give me the desired result.
As I do not have a Windowsversion in english I enclose the corresponding Printscreen.

My solution gives the result shown in column B. (modificationtime = probabli when I resized the picture last)
Your solution gives the result in column D (creationtime = the time I copied the picture).

What I want is the time shown in Column A or C (the time th picture was taken)....
... somehow I suspect Windows looks at the EXIF of the picture when creating this Datetime. But I do not know where it is stored.

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: how to get the original filedate not date modifyed
« Reply #3 on: November 07, 2017, 06:21:52 pm »
I haven't checked ASerge's code, but took my inspiration (read: shameless copy) from StackOverFlow.

Code: Pascal  [Select]
  1. program test;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   WIndows, SysUtils;
  7.  
  8.  
  9. procedure ReportTime(const Name: string; const FileTime: TFileTime);
  10. var
  11.   SystemTime, LocalTime: TSystemTime;
  12. begin
  13.   if not FileTimeToSystemTime(FileTime, SystemTime)
  14.   then RaiseLastOSError;
  15.  
  16.   if not SystemTimeToTzSpecificLocalTime(nil, SystemTime, LocalTime)
  17.   then RaiseLastOSError;
  18.   WriteLn(Name, ': ', DateTimeToStr(SystemTimeToDateTime(LocalTime)));
  19. end;
  20.  
  21. procedure ReportFileTimes(const FileName: string);
  22. var
  23.   fad: TWin32FileAttributeData;
  24. begin
  25.   if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad)
  26.   then RaiseLastOSError;
  27.   WriteLn(FileName);
  28.   ReportTime('Created' , fad.ftCreationTime);
  29.   ReportTime('Modified', fad.ftLastWriteTime);
  30.   ReportTime('Accessed', fad.ftLastAccessTime);
  31. end;
  32.  
  33. begin
  34.   ReportFileTimes('test.exe');
  35. end.
  36.  

Perhaps that is able to help you out ?
« Last Edit: November 07, 2017, 06:23:23 pm by molly »

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #4 on: November 07, 2017, 06:37:39 pm »
Thanks.. it looks similar to Serges code...

Code: Pascal  [Select]
  1.   ReportTime('Created' , fad.ftCreationTime);
  2.   ReportTime('Modified', fad.ftLastWriteTime);
  3.   ReportTime('Accessed', fad.ftLastAccessTime);
  4.  
  5. here I need someting like (wherever Windows stores this)
  6.  
  7.   ReportTime('original' , fad.ftOriginalTime); ???
  8.   ReportTime('EXIF', fad.ftEXIFTime);  ???
  9.   ReportTime('camera', fad.ftCameraTime); ???

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: how to get the original filedate not date modifyed
« Reply #5 on: November 07, 2017, 06:47:16 pm »
Ah ok.

Just to make sure: are you are talking about metadata stored inside the file itself ?

In case it is then this can't be retrieved with the functions showed so far. It depends on the fileformat and how the metadata is stored inside the file.

Seeing that you mentioned exif, afaik means that you would either require to use a exif library that examines the file or use special windows shell api functionality (the latter i do not have a ready to go example for as ti depends on the installed explorer extension(s)).

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #6 on: November 07, 2017, 07:01:28 pm »
You see my problem alright!

But Windows stores this date in the field: date taken (rightclick on a JPG in explorer --- Properties -- Details  -- Title: Origin) and makes it to its (main)date.  -- or look at my printscreen above.
Now is just the question how to get hold of that.

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #7 on: November 07, 2017, 07:08:35 pm »
Hi molly

again you are right... sorry I did not finish reading correctly.
So if somebody has a API-solution I gladly apprechiate that.
On the other hand I can redesign my workflow, as I'm not verry good in API-Calls and verry new to lazarus..

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #8 on: November 07, 2017, 11:42:46 pm »
Finally I fount the right property - it is System.Photo.DateTaken

https://msdn.microsoft.com/en-us/library/windows/desktop/bb760410(v=vs.85).aspx

but how do I implement an API-Call??? I did not find an example...

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: how to get the original filedate not date modifyed
« Reply #9 on: November 08, 2017, 01:56:19 am »
@ratmalwer:
You can try the port of DExif library, which afaik has the additional feature of being platform agnostic. If you download the sources then there is a pdf that has all relevant information listed to tell you how to use the library.

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #10 on: November 08, 2017, 01:22:34 pm »
Thanks for this hint. This could be a possibility, but I suppose it is rather slow on a big amount of pictures.

Now I installed the package: DExif and got the basic-example running - I get a heap-error closing the app. So I have no plan what to do?!

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyFileName : String;
  4.   ImgData: TImgData;
  5. begin
  6.      if OpenDialog1.Execute then
  7.    begin
  8.      MyFileName := OpenDialog1.Filename;
  9.    end;
  10.   if FileExists(MyFileName) then begin
  11.     ValueListEditor1.Clear;
  12.     Image1.Proportional:= True;
  13.     Image1.Stretch:= True;
  14.     Image1.Picture.LoadFromFile(MyFileName);
  15.     ImgData:= TImgData.Create();
  16.     try
  17.       if ImgData.ProcessFile(MyFileName) then begin
  18.         if ImgData.HasEXIF then begin
  19.           ValueListEditor1.InsertRow('Camera Make',
  20.             ImgData.ExifObj.CameraMake,True);
  21.           ValueListEditor1.InsertRow('Camera Modell',
  22.             ImgData.ExifObj.CameraModel,True);
  23.           ValueListEditor1.InsertRow('Picture DateTime',
  24.             FormatDateTime(ISO_DATETIME_FORMAT, ImgData.ExifObj.GetImgDateTime),True);
  25.         end
  26.         else
  27.           ValueListEditor1.InsertRow('No EXIF','No Data',True);
  28.       end
  29.       else
  30.         ValueListEditor1.InsertRow('No EXIF','Processdata',True);
  31.     finally
  32.       ImgData.Free;
  33.     end;
  34.   end;
  35. end;

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: how to get the original filedate not date modifyed
« Reply #11 on: November 09, 2017, 02:11:16 am »
Thanks for this hint. This could be a possibility, but I suppose it is rather slow on a big amount of pictures.
How do you think that Explorer retrieves this information ? Exactly in a similar manner  :)

The only thing that explorer could perhaps do better (due to its integration) is caching the results in the background. That advantage is gone the moment you start to handle a big amount of files, as cache runs dry.

Quote
Now I installed the package: DExif and got the basic-example running - I get a heap-error closing the app. So I have no plan what to do?!
Post your project or create a simple example project showing that heaptrace error. You can publish your project inside lazarus and zip up the directory/files where you published the files into. You can attach that zip-file to a post on the forum

I made a simple example using FPC, based on the code you showed and i did not experience any leakage. That suggests that you have some leakage somewhere else inside your code and is probably not related to your use of DExif library.

fwiw: this is the output from my fpc test example (using the nokia test jpg that came with DExif):
Code: [Select]
> test
Camera Make      : Nokia
Camera Model     : 6300
Picture DateTime : 2017-09-11 09:57:02
Heap dump by heaptrc unit
1559 memory blocks allocated : 141850/158056
1559 memory blocks freed     : 141850/158056
0 unfreed memory blocks : 0
True heap size : 163840 (96 used in System startup)
True free heap : 163744
Note that heaptrace does produce a message (i did not turned that off), but is telling that there are zero unfreed memory blocks (meaning everything went ok).
« Last Edit: November 09, 2017, 02:28:59 am by molly »

ratmalwer

  • New member
  • *
  • Posts: 41
Re: how to get the original filedate not date modifyed
« Reply #12 on: November 09, 2017, 02:51:10 pm »
Hi molly
thanks that you care.

I agree, that windows gets the information from the Exif. But I think it stores it in System.Photo.DateTaken for fast access in Windows-explorer. Else it would take ages opening a folder contining hundreds of pictures. Thats the reason I fancy this option.

On the other hand I could use the DExif to display more information in my app.
So I enclose my project (Basic-sample sligtly modifyed) and the errormessage. Please tell me if thats suffitient.


molly

  • Hero Member
  • *****
  • Posts: 2345
Re: how to get the original filedate not date modifyed
« Reply #13 on: November 09, 2017, 05:02:29 pm »
thanks that you care.
Please tell me if thats suffitient.

You're welcome, and at the same time you make my 'job' easy. The picture alone was sufficient  :)

Take a very good look at your screen-shot. It says: "0 unfreed memory blocks : 0"

As i wrote in my previous post, that means that everything went ok. There are no memory leaks in your project.

You might ask yourself then why on earth does heaptrace display a message when there are no leaks. The answer to that is simple: You either make use of heaptrace or you don't.

By default heaptrace outputs its collected information. You can change that behaviour by setting the variable GlobalSkipIfNoLeaks to true on your program startup (form.create should be ok).

afair it might be that you have a Lazarus with an older FPC compiler, in which case this variable is not available (so that you are unable to influence this behaviour of heaptrace). What you could  do then is opt for upgrading your Lazarus installation.

mai

  • Full Member
  • ***
  • Posts: 133
  • truther
Re: how to get the original filedate not date modifyed
« Reply #14 on: November 09, 2017, 07:51:57 pm »
maybe use statx xstat (search my other post)

make syscall to use it

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus