PDA

View Full Version : file renaming on windows



jdd81
2nd October 2007, 00:55
Hi!

I am trying to move a file using QFile::rename() but the results are more like a copy operation. The file gets copied to the new file name, but it still seems to exist at the old file name as well. I have also tried QDir::rename with the same result.

Calling QFile:: exists shows that the original file exists, but using QFile::remove to delete the original file fails with an error code of QFile::NoError. You can delete the file manually.

Using QFileInfo:: permission, it looks like the owner, user, and others groups have write permission to the file.

We build our application for both linux and windows, and under Linux the above scenario works as you would expect, and the file is moved from one name to another.

Has anyone else run into this?

Thanks,
Jesse

marcel
2nd October 2007, 07:11
Does rename returns true or false?

jdd81
2nd October 2007, 16:40
The rename returns true.

marcel
2nd October 2007, 16:48
Are you trying "move" the file across partitions/hard drives or the destination is on the same partition as the source?

jdd81
2nd October 2007, 16:51
Same partition / drive. It is actually in the same directory. I want to move the file so I can copy a file to its original name.

wysota
2nd October 2007, 16:53
Maybe you could use QTemporaryFile instead?

jdd81
2nd October 2007, 17:05
Thanks for the suggestion, but I'm not sure that will work the way I want it to.

I am basically working on a save mechanism where there is a saved file, and a working file. On a save operation, I want to move the saved file to a temporary name, copy the working file to the saved file name, and then delete the temporary file on success, or rename it back to the saved file if the copy fails. The rename is key, because copying the working file to the save file is the goal, and the rename will free up the save file name.

wysota
2nd October 2007, 17:10
Check if you are not trying to delete an opened file.

jdd81
2nd October 2007, 17:34
Doh! Yes, that was the problem. The file was being opened with a QLockedFile somewhere else in the program. Funny how that didn't stop the operation on Linux!

Anyway, thank you both very much for your help. I still have some hair left. :)

wysota
2nd October 2007, 19:41
Funny how that didn't stop the operation on Linux!

The reason is simple. Windows uses mandatory locking while Linux uses advisory locking thus it is possible to ignore the lock on Linux but not on Windows (the latter causes inability to reopen a file after a crash of an application holding a lock to the open file).