Real Software Forums
http://forums.realsoftware.com/

Save As Dialogue with CopyFileTo command does not overwrite
http://forums.realsoftware.com/viewtopic.php?f=1&t=41600
Page 1 of 1

Author:  eugenedakin [ Sun Nov 27, 2011 9:57 pm ]
Post subject:  Save As Dialogue with CopyFileTo command does not overwrite

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

Author:  DaveS [ Sun Nov 27, 2011 10:23 pm ]
Post subject:  Re: Save As Dialogue with CopyFileTo command does not overwr

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.

Author:  timhare [ Sun Nov 27, 2011 10:24 pm ]
Post subject:  Re: Save As Dialogue with CopyFileTo command does not overwr

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.

Author:  Jym [ Sun Nov 27, 2011 11:24 pm ]
Post subject:  Re: Save As Dialogue with CopyFileTo command does not overwr

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

Author:  eugenedakin [ Sun Nov 27, 2011 11:31 pm ]
Post subject:  Re: Save As Dialogue with CopyFileTo command does not overwr

Excellent! Thanks Dave, Jym and Tim for your help. :)

Have a good week!

Eugene

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/