How do I get file creation date and / or last modified date and time?

Hello, nice to be here :wink:

My challenge is to read filenames and their creation date/time as well as their last modified date and time.
Using TaifunFile so far, I only get the directory-, and filenames.
Any suggestion, how I can get those other meta data?

Thank you very much.
Regards

Hello Arris

What type of files? Most files, such as text files, csv etc do not store meta data. Image files do - you would need Taifun's Exif extension for that:

EDIT: I stand corrected by TimAI2 re attributes in files such as .txt and .csv - they do store that data!

Hey Chris,

thank you very much for your quick answer.
This is exactly what I need, I'm going to invest those 12€ :wink:

Thanks a lot.

Note that if an image file has been converted from one format to another, Exif data is usually lost or corrupted. That also happens if the image is modified in any way - rotated, resized or edited.

Herewith a quick extension to return created/modified/accessed/size(in bytes) of a file.

uk.co.metricrat.fileattributes.aix (16.9 KB)

Example blocks below show how to get this data from a file in your ASD

image

CODE
package uk.co.metricrat.fileattributes;


import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;

import java.nio.file.*;
import java.nio.file.attribute.*;

public class FileAttributes extends AndroidNonvisibleComponent {

  String output = null;

  public FileAttributes(ComponentContainer container) {
    super(container.$form());
  }

  @SimpleFunction(description = "Returns attributes of a file.")
  public String GetFileAttributes(String directory, String filename) {

    Path file = FileSystems.getDefault().getPath(directory, filename);

    try {
      BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
      String creation = attrs.creationTime().toString();
      String modified = attrs.lastModifiedTime().toString();
      String access = attrs.lastAccessTime().toString();
      long size = attrs.size();
      output = "Created: " + creation + "\nLast Modified: " + modified + "\nLast Accessed: " + access + "\nSize: " + size;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return output;
  }

}
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.