-1

The goal is to set a file's create date from the MATLAB command line with a minimal number of JAVA commands. MATLAB's ability to process JAVA NIO commands is impeded by the auto-boxing needed to make them work transparently, so it is necessary to invoke the array form of their arguments.

Starting from Java 7, one can use java.nio.file.Files.setAttribut and the creationTime attribute. Because commands must be issued from the MATLAB prompt, there is a constraint of JDK 1.7 imposed by the system under test.

UPDATE:

I seek to calculate the new file creation dates in MATLAB as shown below and seek to avoid using FileTime.fromMillis(c.getTimeInMillis()) to set the file creation date (if possible).

QUESTION:

Is there an alternative format to set the date / time = 9/1/18 16:00? filedate was calculated but is unable to be cast to a proper file time.

enter image description here

MATLAB COMMANDS:

p="C:DATA\testfile.txt"

filedate=1000*posixtime(datetime('2018-09-01 16:00','InputFormat','uuuu-MM-dd HH:mm','TimeZone','UTC'))

java.nio.file.Files.setAttribute(p, "creationTime", filedate, javaArray('java.nio.file.LinkOption', 0));
gatorback
  • 1,351
  • 4
  • 19
  • 44
  • The answer about Java 7 specifically shows the value parameter as `FileTime.fromMillis(c.getTimeInMillis())`, so why do you believe a number value (`1535817600000`) would be valid? Use `FileTime.fromMillis(filedate)`. --- Or, in Java 8, use e.g. `FileTime.from(Instant.parse("2018-09-01T16:00Z"))` – Andreas Sep 25 '18 at 20:46
  • Good question: I had read that 1000 * epoch time is the file date. The goal is to calculate all dates times in MATLAB and use JAVA only to set the file create date. Please consider posting an answer with an example: I will be happy to test suggestions and post the results :) – gatorback Sep 25 '18 at 20:56
  • I should close the question as a duplicate of the first link you provided, because the answer is there: The value parameter must be `FileTime.fromMillis(...)`. You just didn't follow the instructions. Delete this superfluous question. – Andreas Sep 25 '18 at 21:04
  • @Andreas Thanks for the guidance: it was necessary to use `java.nio.file.attribute.FileTime.fromMillis(filedate)` to successfully execute the command form MATLAB. – gatorback Sep 25 '18 at 23:29

1 Answers1

0

MATLAB m-function Tested with R2017A with JDK 1.7:

function SetCreateDate(STR_filename, posixdate)
% SetCreateDate(STR_filename, posixdate)
% STR_filename = 'C:\Data\my_testfile.dat'
% posixdate = posixtime(datetime('15-Sep-2018 16:00:01','InputFormat','dd-MMM-uuuu HH:mm:ss','TimeZone','America/New_York'))
filePath = strrep(STR_filename,'\','\\');
p = java.nio.file.Paths.get(filePath,javaArray('java.lang.String', 0));
filedate=posixdate*1000;    % filedate is in milli-seconds
java.nio.file.Files.setAttribute(p, "creationTime", java.nio.file.attribute.FileTime.fromMillis(filedate), javaArray('java.nio.file.LinkOption', 0));
%%%% Additional Filetime Attributes that can be set
%java.nio.file.Files.setAttribute(p, "lastAccessTime", java.nio.file.attribute.FileTime.fromMillis(filedate), javaArray('java.nio.file.LinkOption', 0));
%java.nio.file.Files.setAttribute(p, "lastModifiedTime", java.nio.file.attribute.FileTime.fromMillis(filedate), javaArray('java.nio.file.LinkOption', 0));

Other attributes can be found here:

Name                  Type
-------------------------------
"lastModifiedTime"    FileTime
"lastAccessTime"      FileTime
"creationTime"        FileTime
"size"                Long
"isRegularFile"       Boolean
"isDirectory"         Boolean
"isSymbolicLink"      Boolean
"isOther"             Boolean
"fileKey"             Object

Please upvote this answer only if you are successful using the function.

gatorback
  • 1,351
  • 4
  • 19
  • 44