Page 2 of 2

Re: Renaming files and overwriting old versions

Posted: Thu Sep 03, 2020 4:10 pm
by mwieder
One more idea... a file can't be renamed if it's open.
So any chance your saved file is still open at the point you're trying to rename it?
Can you explicitly close it before the rename action?

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 1:32 am
by Opaquer
I gave it a shot and it still said that it cant' rename the file... I'm beginning to think it's a Windows 10 thing unfortunately :(

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 5:02 am
by mwieder
OK - I was running out of straws to grasp, so I fired up a Windows 7 virtualbox VM and I can replicate the problem you're seeing.
If filed a bug report on it. https://quality.livecode.com/show_bug.cgi?id=22891

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 5:14 am
by Opaquer
Ah, awesome! I didn't even know bug reports were a thing, though it obviously makes a TON of sense - just never had to do one before!

Glad to know I'm not entirely crazy! Thanks for the help and the suggestions!

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 6:00 am
by FourthWorld
Looking back at the original code snippet, it occurs to me no error-checking is being done. We can get useful info by checking "the result", and the specific error ID the OS reports to LC using "sysError", e.g.:

Code: Select all

rename file tOldName to tNewName
if the result is not empty then
   answer the result &"("& sysEror() &")"
   exit to top
end if
Rule for good living #48: Always check "the result" when doing anything with I/O, sockets or files. Too many moving parts, lots of ways things can go wrong. And when you do check that, using sysError can sometimes save a lot of head-scratching.

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 6:42 am
by mwieder
Richard- that's what happens when you don't read the whole thread. Read more and you'll see checking the result.
Or look at the bug report.

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 6:46 am
by FourthWorld
Yeah, I read all of page 2 and only half of page 1. I should make a point of re-reading every single post across all pages before I write here. It'll save all of us time, because I don't have that kind of time. :)

Back to work for me...

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 11:10 am
by AxWald
Hi,
running the code from Marks bug report with the debugger quickly shows that the error is thrown only when an existing "help-Completed.txt" would be overwritten.

Add this before the "rename" line & all works well:

Code: Select all

if there is a file "help-Completed.txt" then delete file "help-Completed.txt"
So:

Code: Select all

rename file "fileA" to "fileB" 
will fail if fileB already exists.

Re-Checking from a Win10 command line shows that this is usual behavior:

Code: Select all

C:\Users\user\Documents>rename help.txt help-Completed.txt
=> first run completes

C:\Users\user\Documents>rename help.txt help-Completed.txt
[Filename exists already, or file couldn't be found]
=> Second run fails
(Error msg in brackets translated by me ...)

So it's no LC bug but a Win "feature".

Have fun.

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 11:22 am
by Klaus
OK, so why not just copy the contents of the old file to the new file?
That will "replace" the old file without problems:

Code: Select all

...
put folderPath & "WX823TZ.txt"  into tOldName
put folderPath & "WX823TZ-COMPLETED.txt"  into NewName

## Now just "swap" contents
put url("file:" & tOldName) into url("file:" & tNewName)
...

Re: Renaming files and overwriting old versions

Posted: Fri Sep 04, 2020 4:04 pm
by mwieder
AxWald - that's not a valid comparison - you're using the Windows commandline application as a test.

The LC engine is calling MoveFileExW(t_temp_path_w32, *t_path_w32, MOVEFILE_REPLACE_EXISTING) in system-file-w32.cpp, and that flag *should* allow for overwriting the existing file.

In dskw32.cpp, on the other hand, the call is to MoveFileExW(*t_old_wstr, *t_new_wstr, MOVEFILE_COPY_ALLOWED|MOVEFILE_WRITE_THROUGH), without the MOVEFILE_REPLACE_EXISTING flag.

I think the missing flag is what's preventing the overwrite on Windows, and causing the failure to do what's documented.

Re: Renaming files and overwriting old versions

Posted: Sat Sep 05, 2020 5:36 am
by Opaquer
That's definitely another way to to do it Klaus, though then you would have to delete the old version if it's still around. Not a huge issue, but definitely doable. The way I ended up going with it for now is with a bunch of if statements checking to see if the sudoku has already been saved/worked on, if it's completed and if it's a sudoku being created by someone instead of being solved, along with a couple of other things :)

Re: Renaming files and overwriting old versions

Posted: Sat Sep 05, 2020 6:17 pm
by mwieder
After LCMark's response in the bug report, I filed a pull request to fix the documentation to show that overwriting will not occur on Windows. I think it might also be possible to fix the engine code to check for all kinds of file locks, etc before issuing the rename command, but I'm not certain that would catch everything.

Re: Renaming files and overwriting old versions

Posted: Sat Sep 05, 2020 6:54 pm
by FourthWorld
Add it to the list of reasons the world will rejoice when Microsoft adopts the Linux kernel. ;)