Login  Register

Re: File creation date in a macro?

Posted by ctrueden on Nov 14, 2008; 3:58pm
URL: http://imagej.273.s1.nabble.com/File-creation-date-in-a-macro-tp3694500p3694503.html

Hi Bill,

Why  in  the  world  can't  Java get creation dates? Is  there  some
> philosophy behind the decision? Or is there a technological obstacle? I
>  just  find  it  very  curious, since  the  creation  date  is much less
>  labile  than  the mod  date,  which  can be  easily  changed by random
>  actions  of  the OS.
>

It's a technological obstacle caused by historical design decisions,
particularly in *nix. Nonetheless, I agree with you that Java *should* have
creation dates, but unfortunately it does not.

The thing is, creation date is not a universal concept among OSes -- both
Windows and Mac OS X support the idea, but Unix does not. Even on Mac OS X,
creation date is a second-class citizen -- since OS X is based on BSD and
BSD does not support it, Apple stores the information in the file's resource
fork, making it more difficult to access.

Given these restrictions, I have written a plugin for determining the
creation date of a file. It is HIGHLY platform dependent. On Windows, it
should work OK on regular files, but not directories (this problem could be
fixed, but I was too lazy). On Mac OS X, you must have the developer tools
installed, or the /Developer/Tools/GetFileInfo command it uses will fail.

If anyone knows another way to get the creation date of a file on Mac OS X
using a system call, I would be interested. However, there is no way to get
creation date with the stat utility, because creation date is not a piece of
*nix file metadata -- you can get ctime, which is the birth of the inode,
but that gets modified every time you modify the file or change the file
permissions, so it's useless.

HTH,
-Curtis

//
// Creation_Date.java
//

import ij.IJ;
import ij.io.OpenDialog;
import ij.plugin.PlugIn;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Creation_Date implements PlugIn {

  public void run(String arg) {
    OpenDialog od = new OpenDialog("Get Creation Date", null);
    File file = new File(od.getDirectory(), od.getFileName());
    Date created = getCreationDate(file);
    Date modified = getModifiedDate(file);
    IJ.showMessage("File = " + file + "\nCreated = " + created + "\nModified
= " + modified);
  }

  public Date getCreationDate(File file) {
    String id = file.getAbsolutePath();
    Date date = null;
    if (IJ.isMacOSX()) {
      String[] cmd = {"/Developer/Tools/GetFileInfo", "-d", id};
      try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader in = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
        String result = in.readLine();
        SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy
HH:mm:ss");
        date = parser.parse(result, new ParsePosition(0));
      }
      catch (IOException exc) { }
    }
    else if (IJ.isWindows()) {
      String[] cmd = {"cmd", "/c", "dir", "/t:C", id};
      try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader in = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
        for (int i=0; i<5; i++) in.readLine();
        String result = in.readLine();
        if (result != null) {
          SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy  hh:mm
aa");
          date = parser.parse(result, new ParsePosition(0));
        }
      }
      catch (IOException exc) { }
    }
    return date;
  }

  public Date getModifiedDate(File file) {
    return new Date(file.lastModified());
  }

}

On Thu, Nov 13, 2008 at 7:26 PM, Bill Mohler <[hidden email]>wrote:

> Why  in  the  world  can't  Java get creation dates? Is  there  some
> philosophy behind the decision? Or is there a technological obstacle? I
>  just  find  it  very  curious, since  the  creation  date  is much less
>  labile  than  the mod  date,  which  can be  easily  changed by random
>  actions  of  the OS.
>
> Thanks,
> Bill
>
>
>
>  Hi Bill & Johannes,
>>
>> You can, however, get the last modified date by calling
>> File.lastModified(),
>> which might be good enough.
>>
>> There is another option, depending on why you need such a date.
>>
>> If what you want to know is the creation date of your dataset, you can use
>> Bio-Formats to query the creation date independent of file format. If
>> there
>> is a creation date present in the metadata, Bio-Formats will use that --
>> otherwise it uses the file's last modified datestamp by default.
>>
>> Below is a plugin that queries this value. You can't easily query
>> standardized metadata fields in a macro yet, but I could add this
>> functionality if there is interest.
>>
>> -Curtis
>>
>> -----------
>> //
>> // Creation_Date.java
>> //
>>
>> import ij.IJ;
>> import ij.io.OpenDialog;
>> import ij.plugin.PlugIn;
>> import java.io.IOException;
>> import loci.formats.FormatException;
>> import loci.formats.IFormatReader;
>> import loci.formats.ImageReader;
>> import loci.formats.MetadataTools;
>> import loci.formats.meta.IMetadata;
>>
>> public class Creation_Date implements PlugIn {
>>
>>  public void run(String arg) {
>>    OpenDialog od = new OpenDialog("Open", null);
>>    String dir = od.getDirectory();
>>    String name = od.getFileName();
>>    if (dir == null || name == null) return;
>>    String id = dir + name;
>>    IFormatReader r = new ImageReader();
>>    IMetadata omexmlMeta = MetadataTools.createOMEXMLMetadata();
>>    r.setMetadataStore(omexmlMeta);
>>    try {
>>      r.setId(id);
>>    }
>>    catch (FormatException exc) {
>>      IJ.error(exc.getMessage());
>>    }
>>    catch (IOException exc) {
>>      IJ.error(exc.getMessage());
>>    }
>>    String creationDate = omexmlMeta.getImageCreationDate(0);
>>    IJ.showMessage("File is: " + id + "\nCreation date is: " +
>> creationDate);
>>  }
>>
>> }
>>
>> On Fri, Nov 7, 2008 at 9:01 AM, Johannes Schindelin <
>> [hidden email]> wrote:
>>
>>   Sorry, forgot to forward to the list...
>>>
>>>  Okay, I misremembered.  Java does not have the concept of a creation
>>> date.
>>>  You'd have to call a program (on Linux, it would be "stat -c %z
>>> <file>"),
>>>  and capture the output.  Unfortunately, there does not seem to be a
>>> simple
>>>  way to do this from ImageJ's macro language or JavaScript (yet).
>>>
>>>  Sorry,
>>>  Dscho
>>>
>>>
>
>