Renaming files and overwriting old versions
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Renaming files and overwriting old versions
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?
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?
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Renaming files and overwriting old versions
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
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
If filed a bug report on it. https://quality.livecode.com/show_bug.cgi?id=22891
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Renaming files and overwriting old versions
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!
Glad to know I'm not entirely crazy! Thanks for the help and the suggestions!
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Renaming files and overwriting old versions
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.:
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.
Code: Select all
rename file tOldName to tNewName
if the result is not empty then
answer the result &"("& sysEror() &")"
exit to top
end if
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Renaming files and overwriting old versions
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.
Or look at the bug report.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Renaming files and overwriting old versions
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...

Back to work for me...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Renaming files and overwriting old versions
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:
So: will fail if fileB already exists.
Re-Checking from a Win10 command line shows that this is usual behavior:
(Error msg in brackets translated by me ...)
So it's no LC bug but a Win "feature".
Have fun.
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"
Code: Select all
rename file "fileA" to "fileB"
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
So it's no LC bug but a Win "feature".
Have fun.
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: Renaming files and overwriting old versions
OK, so why not just copy the contents of the old file to the new file?
That will "replace" the old file without problems:
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
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.
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Renaming files and overwriting old versions
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
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Renaming files and overwriting old versions
Add it to the list of reasons the world will rejoice when Microsoft adopts the Linux kernel. 

Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn