3

Here's the code I have right now

/* Rename existing project files to .old */
if (FileIOUtil::fileExists(dest, outFileName))
{
  QFile oldFile(outFileName);
  QFileInfo fileInfo; fileInfo.setFile(oldFile);
  QDateTime created = fileInfo.lastModified();
  FileIOUtil::mvFile(dest, outFileName,
                     dest, outFileName + ".old" + created.toString());
}

Note: mvfile works like the unix command mv. It just moves a file to a new name.

However, this renames my project.c to project.c.old.Thu Jan 1 01:00:00 1970. I'm pretty sure the files I'm trying to rename aren't that old ;)

Any ideas why I'm getting the epoch as a result?

Arnab Datta
  • 5,356
  • 10
  • 41
  • 67
  • Your `created` calculation seems to be correct but too verbose. You can remove `oldFile` and `fileInfo` variables and use `QDateTime created = QFileInfo(outFileName).lastModified();`. Do system utilities display last modified time of this file correctly? – Pavel Strakhov Jun 27 '13 at 12:50
  • @Riateche yes it does. – Arnab Datta Jun 27 '13 at 12:58

1 Answers1

4

I had to modify the following line:

QFile oldFile(outFileName);

to

QFile oldFile(dest + outFileName);

Or as @Riateche mentioned in his comment, remove oldFile and fileInfo variables completely and do:

QFileInfo(dest+outFileName).created();
Arnab Datta
  • 5,356
  • 10
  • 41
  • 67
  • 1
    Yes, it's not surprising because if you specify non-absolute path to `QFileInfo`, it's treated as relative to the current working directory. It seems that `FileIOUtil` class prepends `dest` string internally. I didn't notice that and thought that `outFileName` is already absolute because `mvFile` works on it. – Pavel Strakhov Jun 27 '13 at 13:09