Copyfile doesn't preserve timestamp

[status, msg, msgID] = copyfile(filePath1, filePath2);
This line copies a file from a local directory into a USB drive, but the timestamp is not preserved, and the current time is used instead.
Why does this happen if the documentation says the opposite?
MATLAB R2019b, macOS High Sierra 10.13.6

4 comentarios

dpb
dpb el 19 de Jul. de 2020
Maybe OS problem? Don't have Mac but symptom doesn't appear on R2019b on Win7 which would seem to imply it's not inside the TMW-specific code. It's builtin so can't see what they did, but one presume it's just passing the arguments to the OS, anyways...
Image Analyst
Image Analyst el 19 de Jul. de 2020
Windows has 3 dates: modified date, created date, and accessed date. Maybe macOS is the same. Have you looked at all 3 dates?
Stephen23
Stephen23 el 20 de Jul. de 2020
Vincenzo'S "Answer" moved here:
I also observed that this behaviour is somewhat file-dependent.
On one file I get the same modified date as the original copy, on other ones (a .dmg file, a .png file) I get the current time as the modified date.
Strangely, the creation date is set for both at an arbitrary date, to which I see no link with the original copies.
Stephen23
Stephen23 el 20 de Jul. de 2020
Vincenzo's "Answer" moved here:
Let me disagree here. The documentation doesn't specify an OS-dependent compatibility, so if it's an OS-dependent issue, it means it's an OS-dependent bug in TMW-specific code. It would be useful to compare results to analyze different behaviours and possibly raise a bug fix request to The Mathworks.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 20 de Jul. de 2020

0 votos

Windows has 3 dates: modified date, created date, and accessed date. Maybe macOS is the same. For Windows 10, the modified date is the same as it was. The Created date and the Accessed date of the new file are the current time that copyfile() was executed. The documentation only says "After a successful copyfile operation, the timestamp for the new file is the same as the timestamp for source." I agree this is ambiguous when the operating system maintains 3 dates for the files. It could be more explicit on what "timestamp" means especially when only one of the timestamps is the same and two will be different. You could click the single star at the bottom of the documentation and then put in your suggestion (like I just did). I know people do read those suggestions.
MATLAB also does not seem to have a touch function so perhaps you can use system() to run such a command after you run copyfile(), if your operating system has such a touch function.

7 comentarios

Vincenzo
Vincenzo el 20 de Jul. de 2020
Editada: Vincenzo el 20 de Jul. de 2020
Yes, as a workaround I ended up doing exactly that, a system() call to the macOS terminal command
touch -mf YYYYMMDDHHmm filePath
after copyfile() has been executed.
Image Analyst
Image Analyst el 20 de Jul. de 2020
And does that change all 3 timestamps, or only 1 of them? Since you do it, I assume it's changing all 3 but I just want to be clear on that.
Vincenzo
Vincenzo el 20 de Jul. de 2020
From what I see, the touch command on macOS modifies only the "last modified" timestamp.
Image Analyst
Image Analyst el 20 de Jul. de 2020
Which timestamp(s) do you need to be kept the same, and why? What's wrong with the way it's doing it now? What app is going to be inspecting those timestamps? What is it expecting to see?
Vincenzo
Vincenzo el 21 de Jul. de 2020
Editada: Vincenzo el 21 de Jul. de 2020
I'm building a folder synchronization app, and I want a synchronized file to keep its last modified timestamp.
Therefore what I need is exactly what the "touch" terminal command does. The workaround of using the system() call it's ok from a functional point of view, it's nonetheless confusing that the MATLAB function copyfile() does not handle this, and even more strangely that it handles the last modified timestamp only for some files and not for others.
I think there is definitely something to inspect in the behaviour of the copyfile() function on macOS. Alternatively, it has to be clarified in the documentation that the compatibility on macOS is limited.
dpb
dpb el 21 de Jul. de 2020
If you can create a reproducible test case that illustrates a problem, submit a formal bug report to TMW. Answers is NOT official support. See "Contact Us" (the old-style telephone receiver) icon at top of page or email support@mathworks.com
Walter Roberson
Walter Roberson el 21 de Jul. de 2020
For folder synchronization you should consider system() of rsync

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 19 de Jul. de 2020

Comentada:

el 21 de Jul. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by