Renaming files and overwriting old versions

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Renaming files and overwriting old versions

Post by mwieder » Thu Sep 03, 2020 4:10 pm

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?

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Renaming files and overwriting old versions

Post by Opaquer » Fri Sep 04, 2020 1:32 am

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 :(

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Renaming files and overwriting old versions

Post by mwieder » Fri Sep 04, 2020 5:02 am

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

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Renaming files and overwriting old versions

Post by Opaquer » Fri Sep 04, 2020 5:14 am

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!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Renaming files and overwriting old versions

Post by FourthWorld » Fri Sep 04, 2020 6:00 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Renaming files and overwriting old versions

Post by mwieder » Fri Sep 04, 2020 6:42 am

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Renaming files and overwriting old versions

Post by FourthWorld » Fri Sep 04, 2020 6:46 am

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...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Renaming files and overwriting old versions

Post by AxWald » Fri Sep 04, 2020 11:10 am

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.
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!

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Renaming files and overwriting old versions

Post by Klaus » Fri Sep 04, 2020 11:22 am

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)
...

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Renaming files and overwriting old versions

Post by mwieder » Fri Sep 04, 2020 4:04 pm

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.

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Renaming files and overwriting old versions

Post by Opaquer » Sat Sep 05, 2020 5:36 am

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 :)

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Renaming files and overwriting old versions

Post by mwieder » Sat Sep 05, 2020 6:17 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Renaming files and overwriting old versions

Post by FourthWorld » Sat Sep 05, 2020 6:54 pm

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

Post Reply