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

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

Renaming files and overwriting old versions

Post by Opaquer » Wed Sep 02, 2020 4:13 pm

Hey guys, me again with my sudoku app!

I've hit a bit of a wall when it comes to file manipulation. I've got everything else working so far, but I want to make my app change the name of the text file associated with the saved sudoku to something that says completed in it, so I can archive it away from the user, but still be available if they want it down the line.

For example, I have a sudoku saved to a file "WX823TZ.txt", and when the user completes it, I want my stack to change that file to "WX823TZ-COMPLETED.txt"

Now, I read the file and close it, so that's no issue, and when the sudoku gets completed I'm able to do the following code:

Code: Select all

      if completed and Creator=0 then
         put folderPath into NewFileName -- folderPath, unsurprisingly, contains the folder path of the current sudoku
         replace ".txt" with "-COMPLETED.txt" in NewFileName
         rename folderPath to NewFileName
      end if
Now, this does exactly what you'd expect- renames the file with the new file (yay!), but the issue is if this gets called again, according to the dictionary entry for rename: "Warning: If a file with the same file path as the newPath already exists, that file will be overwritten without confirmation."

However when I try, it just makes the same file name as before, WX823TZ.txt, and WX823TZ-COMPLETED.txt is also still there for some reason.

Am I missing something really obvious with the rename command, because as far as I can tell, there should only ever be the WX823TZ-COMPLETED.txt file, no matter how many times the handler gets called, no?

Many thanks in advanced!

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

Re: Renaming files and overwriting old versions

Post by Klaus » Wed Sep 02, 2020 4:56 pm

Hi Opaquer,

use the RENAME command:

Code: Select all

...
put folderPath & "WX823TZ.txt"  into NewOldName
put folderPath & "WX823TZ-COMPLETED.txt"  into NewNewName

## Now only change the filename with RENAME:
rename file tOldName to tNewName
...
Best

Klaus

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Wed Sep 02, 2020 5:20 pm

Hey Klaus

I'm already using the rename command, but the dictionary is saying it will overwrite files that have the same filename as the new file, which is what I'm after, but for some reason mines not :-(

Many thanks

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

Re: Renaming files and overwriting old versions

Post by Klaus » Wed Sep 02, 2020 5:24 pm

Oops, sorry completely missed that. :oops:

But what exactly is the problem?
Given you have file xyz.txt and renamed it to xyz-completed.txt.
What do you you do now and what does or does not happen?

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 » Wed Sep 02, 2020 5:25 pm

What are the modification dates of each file?

And do you have an other part of the program that writes to the file without also renaming it?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Wed Sep 02, 2020 5:47 pm

All good Klaus, it happens :-)! And the issue is that since these are save files, if a sukdo is completed I want to rename it as completed so that when a user is searching through their saves, I can filter out saves that are completed, unless they choose to see them.

The issue though is if someone does choose to view a completed sudoku (for whatever reason), and they either save manually or my app saves automatically (when they close the app it auto saves), what I want it to do is for the save handler to save the sudoku, check if it's completed and if so, rename it to have the completed bit at the end. However, instead it's renaming it the first time around, but every time after that it saves a new file with the old name (without the completed bit), and instead of overwriting it, just leaves both files there, as if ones completed and ones not

Also, FourthWorld, when the file gets renamed, the modification date changes to the current date and time accordingly, but once a new save file has been made and I go to rename it again, it just updates the modification date to the current time for the old, uncompleted, file, and doesn't touch the new completed one.

My save handler in theory overwrites the files, but this is all part of that handler.

Basically I have

Save file
If complete then
Rename file
End if

So I can't see any reason why it won't overwrite it

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 1:58 am

Opaquer- what you've got there should work.
Stick a breakpoint after the rename command and verify that it got renamed and there's no un-COMPLETED.txt file in the folder.
If so then somewhere else in your code afterwards you're saving the file again.

Also, you might check the result of the rename command to see if there's an error:

Code: Select all

rename file tOldName to tNewName
put "rename returned:" && the result & cr after msg

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Thu Sep 03, 2020 3:31 am

I gave it a shot, and when it renamed the file, it didn't say anything (which seemed good), but when it tried to rename it and couldn't, it just said "can't rename file", but nothing else? Maybe the dictionary is wrong and it can't overwrite files? If so, I can just change my code a bit so that there's some handy if statements to figure out if it should rename or save, but I just thought having the rename would make it easy if it could overwrite files!

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 3:56 am

No problem overwriting files with rename. It works as advertised.
You might check out the actual contents of tOldName and tNewName when you're in the debugger.
I get the "can't rename" error if I give it an invalid oldname. Obviously can't rename a file it can't find.

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Thu Sep 03, 2020 4:25 am

Hmm... I feel like I'm going mad here...

So, I did this:

Code: Select all

         -- do stuff here to save file at location folderPath
         if there is a file folderPath then
            answer 1
         else
            answer 2
         end if
         if there is a file NewFileName then
            answer 3
         else
            answer 4
         end if
         rename folderPath to NewFileName
         answer the result
As this would be an easy way to tell what's going on. I thought maybe because if there's a completed version and it saves as an uncompleted name, maybe it hasn't had enough time to actually save before I ask it to rename it, which is why it's failing.

First time I ran it when there was an uncompleted text file only, it answered 1 (the uncompleted text file exists), then 4 (the completed text file doesn't exist), then the result was blank (it has renamed it fine)

The next time I ran it, it only had the completed text file and there was no uncompleted one. It did the saving of stuff and answered 1, showing that the uncompleted file was there, then answered 3 to the completed one being there too, and then the result was "can't rename file"! So it's not even like there's not enough time after creating the file to rename it, it can instantly see that the file is there and exists??

I think I've gone crazy with it all to be honest!

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 5:14 am

Hmmm... could this be OS-related? What platform is this on?

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Thu Sep 03, 2020 5:20 am

Could be - I'm coding it in Windows 10, and the folder I'm trying to access isn't anything crazy, just my documents folder

I even just tried with a different version of Livecode just in case it wasn't working and got the same results? What OS are you on?

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 5:29 am

Well, I'm on linux of course :shock:
I could try this on OSX as well, but I don't think that would help you.

Of course you could always resort to "if the *completed* file already exists then delete it before trying a rename" approach, but it really seems like this should be working. I'm down to thinking it's Windows10-related, but someone else will have to come along and prove or disprove that theory.

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 6:01 am

Just had a quick look at the engine code, and it's pretty gnarly but we appear to be doing all the right things.
We're using the MOVEFILE_REPLACE_EXISTING flag, which should allow for renaming in all situations. I suppose it's possible that the permissions on your Documents folder is somehow weird, but I think that's a long shot.

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

Re: Renaming files and overwriting old versions

Post by Opaquer » Thu Sep 03, 2020 8:15 am

Maybe it's something with windows. I want to do release my app on android anyway, but I'm slightly worried that if it doesn't work on windows, maybe on some phones it may not work as well, so I might stick to the "if completed" setup instead!

Thanks for all the help for it!

Post Reply