Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Sep 21, 2019 2:11 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Save As Dialogue with CopyFileTo command does not overwrite
PostPosted: Sun Nov 27, 2011 9:57 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Hello Everyone,

I have created a sample SaveAs dialog example on my website at : http://drdakin.tripod.com/REALbasic/REALbasic.htm with the following code

Dim dlg as New SaveAsDialog
Dim f,g as FolderItem

g = GetFolderItem("Hello.txt") 'get the file hello.txt
if g.exists then msgbox "f exists" 'make sure the file exists
f=dlg.ShowModal() 'save the file with the ShowModal Dialogue

If f <> Nil then
g.CopyFileTo(f)
msgbox "file saved" 'The file 'should' be saved
Else
msgbox "file not saved"
//user canceled
End if


The Saveas dialogue and CopyFileTo command work well when the file is being copied to a folder which does not have the same filename. However, the file is not overwritten when the current file exists. Here are the steps to reproduce the issue:

1 ) Download the example 'SaveAs Dialog' file from http://drdakin.tripod.com/REALbasic/REALbasic.htm
2 ) Unzip and run the program
3 ) Press the 'save dialogue file' button
4 ) Same the file as 'Test.txt' in a directory (it doesn't matter which directory, just pick one)
5 ) Open the Test.txt file and it should have the word "Hello". Change the Test.txt word to "Hello there"
6 ) Save and close the Test.txt file and rerun the program.
7 ) Rerun the program and attempt to save over the Text.txt file. You should see the messagebox saying "Test.txt already exists. Do you want to replace it?", and press yes. A dialog box will say "file saved".
8 ) Open Test.txt. The text should say "Hello" from the Hello.txt file, but it still says "Hello there".

Why was the file not overwritten? Is there other code which I need to integrate into the Save As Dialog Box to overwrite an existing file? If so, how?

Thanks for your help, :)

Eugene
Windows 7
REALStudio 2011 r3


Top
 Profile  
Reply with quote  
 Post subject: Re: Save As Dialogue with CopyFileTo command does not overwr
PostPosted: Sun Nov 27, 2011 10:23 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
Just add the one line


Dim dlg as New SaveAsDialog
Dim f,g as FolderItem

g = GetFolderItem("Hello.txt") 'get the file hello.txt
if g.exists then msgbox "f exists" 'make sure the file exists
f=dlg.ShowModal() 'save the file with the ShowModal Dialogue

If f <> Nil then
IF f.EXISTS THEN F.DELETE // **** ADD THIS LINE **** //
g.CopyFileTo(f)
msgbox "file saved" 'The file 'should' be saved
Else
msgbox "file not saved"
//user canceled
End if



Per the LR

Quote:
If Destination is a folder, then the FolderItem is copied into Destination. If Destination is a file and the file already exists, the copy is aborted. You need to delete the existing file first. If there is an error, the LastErrorCode property contains an error code.

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Save As Dialogue with CopyFileTo command does not overwr
PostPosted: Sun Nov 27, 2011 10:24 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You should delete the file if it already exists. The dialog warns the user, but does not delete the file. It's up to you to do so.


Top
 Profile  
Reply with quote  
 Post subject: Re: Save As Dialogue with CopyFileTo command does not overwr
PostPosted: Sun Nov 27, 2011 11:24 pm 
Offline

Joined: Sat Oct 01, 2005 5:19 pm
Posts: 3216
Then check to make sure the file got deleted

If f.Exists then f.Delete

if f.Exists then
msgbox "The File is currently in use or you don't have the permissions to delete this file."
Return
End if


Top
 Profile  
Reply with quote  
 Post subject: Re: Save As Dialogue with CopyFileTo command does not overwr
PostPosted: Sun Nov 27, 2011 11:31 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Excellent! Thanks Dave, Jym and Tim for your help. :)

Have a good week!

Eugene


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group