Modify the file. The date of creation

Hi,
I’m trying to modify file like this:

  1. xml->writeTo(file, {});
    or like this
  2. file.replaceWithText(xml.release()->toString());

Both options update the date of creation of this file.

Are there any ways how to update the content of the file without changing the date of creation?

Thanks

If your write operations mess with the date of creation, couldn’t you just change it back afterwards?

something like

auto realCreationTime = file.getCreationTime();

// do your write to file operations here

file.setCreationTime(realCreationTime);
3 Likes

I don’t why, but this method didn’t change creation date too

But in general, how it can work?

setCreationTime method looks like this:

bool File::setCreationTime (Time t) const { return setFileTimesInternal (0, 0, t.toMilliseconds()); }

Inside setFileTimesInternal we see:

if ((modificationTime != 0 || accessTime != 0) && juce_stat (fullPath, info))

But in the previous method, we set modificationTime and accessTime to 0

So it’s always false;

Interesting… I don’t know. I’m sure someone more experienced with file management in Juce will know the answer :slightly_smiling_face:

You can create a FileOutputStream for the target file using File::createOutputStream(), and write the data using FileOutputStream::write(). This will preserve the metadata, but is quite a bit more annoying and error-prone than using methods like replaceWithText().

You’re correct about the logic in File::setCreationTime(). It looks like this isn’t an issue on Windows, as its setFileTimesInternal() implementation doesn’t perform the (modificationTime != 0 || accessTime != 0) check. But for other systems that use the POSIX implementation, setCreationTime() will always fail due to this check.

2 Likes

Thank you. Will try.

Any comments from JUCE team about these methods?