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

Eliminating "extra" steps in saving a file.
http://forums.realsoftware.com/viewtopic.php?f=1&t=48042
Page 1 of 2

Author:  newyorkpaulie [ Sun Jun 02, 2013 5:36 am ]
Post subject:  Eliminating "extra" steps in saving a file.

When I want to save a file I've updated using one of my programs a button press calls the code here:

dim f as folderitem
dim i as integer
dim stream as binarystream
dim theStoringFolder as String
theStoringFolder = "/home/Documents/"
f=getsaveFolderItem("", theStoringFolder + "myFile")
if f<> nil then
stream=f.createbinaryfile("myfiletype")
stream.WriteLong myListBox.ListCount
for i=0 to myListBox.listcount-1
stream.writePString myListBox.list(i)
next
stream.close
end if


I then have to press a key given this choice:
[Save] [Cancel]

And then I have to press another key given this choice:
'/home/Documents/myFile' already exists.
Overwrite it?
[Yes] [No]


Is there a way to eliminate these 2 key presses by adding instructions to my (above) code?

Author:  kermit [ Sun Jun 02, 2013 12:15 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

instead of

f=getsaveFolderItem("", theStoringFolder + "myFile")

use

f=getFolderItem("", theStoringFolder + "myFile")

Author:  classone [ Sun Jun 02, 2013 12:43 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

To eliminate this step,
Quote:
And then I have to press another key given this choice:
'/home/Documents/myFile' already exists.
Overwrite it?
[Yes] [No]

Do...
stream=binaryStream.Create(f, True)
//You should be using this form anyway


But be aware that your previously existing file will always be overwritten.
YOu should also check for
stream <> Nil
before proceeding to use the stream object.

Author:  newyorkpaulie [ Sun Jun 02, 2013 12:56 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

kermit wrote:
instead of

f=getsaveFolderItem("", theStoringFolder + "myFile")

use

f=getFolderItem("", theStoringFolder + "myFile")
Is more needed here than a simple swap of names? Just making the change you suggested results in the following error msg.
Parameters are not compatible with this function
f=getFolderItem("",theStoringFolder + "myFile")

Author:  classone [ Sun Jun 02, 2013 1:07 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

Eliminate that first argument passed.
f=getFolderItem(theStoringFolder + "myFile")

But, adding the caveat that there are many things which can go wrong with (theStoringFolder + "myFile")
Firstly, I don't know exactly what "thestoringFolder" contains, but it should proibably be
theStoringFolder.Child("myFile")
Secondly, if "theStoringFolder" is a complicated path, then it can fail at many levels.
Use .Child to work down through your folder heirarchy checking <> Nil and .Exists at each level.

Author:  newyorkpaulie [ Sun Jun 02, 2013 1:13 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

classone wrote:
To eliminate this step,
Quote:
And then I have to press another key given this choice:
'/home/Documents/myFile' already exists.
Overwrite it?
[Yes] [No]

Do...
stream=binaryStream.Create(f, True)
//You should be using this form anyway


But be aware that your previously existing file will always be overwritten.
YOu should also check for
stream <> Nil
before proceeding to use the stream object.
I changed my "stream=..." line to the one you suggested. The program runs exactly as before and still requires the 2 "extra" presses I'm trying to eliminate. Thanks for your help - is there something else I am leaving out?

Author:  classone [ Sun Jun 02, 2013 1:34 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

In short, yes, there's something you are doing wrong.
Post your new, complete code and I'll have a look later this evening.

Author:  newyorkpaulie [ Sun Jun 02, 2013 1:43 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

classone wrote:
In short, yes, there's something you are doing wrong.
Post your new, complete code and I'll have a look later this evening.

Here are the "actual" lines. I had simplified them in my original post...

dim f as folderitem
dim i as integer
dim stream as binarystream
dim theStoringFolder as String
theStoringFolder = "/home/paulie/Documents/"
f=getsaveFolderItem("", theStoringFolder + "1WeightLst")
if f<> nil then
stream=binaryStream.Create(f, True)
stream.WriteLong myListBox.ListCount
for i=0 to myListBox.listcount-1
stream.writePString myListBox.list(i)
next
stream.close
end if
quit


Thanks for taking the time to offer your help!
Paul

Author:  kermit [ Sun Jun 02, 2013 1:51 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

getsaveFolderItem...

displays a dialog that asks the user what file name to use, and allows them to change it.
It also carries logic to ask those questions.

Use getFolderItem() instead.

It only wants one parameter: the name of the output file.
f=getFolderItem(theStoringFolder + "myFile")


But since it is documents, the better approach is:

f = specialfolder.documents.child("myfile")

Author:  tomis [ Sun Jun 02, 2013 1:52 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

dim theStoringFolder as String
theStoringFolder = "/home/paulie/Documents/"
f=getsaveFolderItem("", theStoringFolder + "1WeightLst")


Don't hard codes paths like this. If you're accessing a common location like this then use:

dim f as FolderItem
f=SpecialFolder.Documents.child("1WeightLst")

Author:  classone [ Sun Jun 02, 2013 6:23 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

Please pardon if this sounds a bit short, but kermit told you about 6 posts ago to use GetFolderItem instead of getSaveFolderItem. You ignored his advice and then wonder why you're still getting the same results.

Author:  newyorkpaulie [ Sun Jun 02, 2013 7:12 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

classone wrote:
Please pardon if this sounds a bit short, but kermit told you about 6 posts ago to use GetFolderItem instead of getSaveFolderItem. You ignored his advice and then wonder why you're still getting the same results.
I appreciate your help, but look again (about 8 posts above). I DID try kermit's suggestion and reported my results. Do you now understand why I am wondering why etc... ? Sorry if this sounds petulant, but I took the bait.

Author:  classone [ Sun Jun 02, 2013 7:46 pm ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

If I correctly understand your needs, this code should do what you want. Please try it and let me know your results.
dim f As FolderItem = SpecialFolder.Documents
dim stream As BinaryStream

if f = Nil or not f.Exists Then
MsgBox "There is no documents folder."
Return
end if

f = f.Child("1WeightLst")
if f <> Nil Then
stream = BinaryStream.Create(f, True) //creates a file named '1WeightLst' and overwrites any existing file
if stream <> nil Then
dim u As Integer = myListBox.listCount-1
for i As Integer = 0 to u
stream.WritePString myListBox.list(i)
Next
stream.Close
end if
end if

Author:  newyorkpaulie [ Mon Jun 03, 2013 5:15 am ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

classone - I tried your code (thank you for doing this for me!) but it triggers the error msg. "There is no documents folder."

Author:  classone [ Mon Jun 03, 2013 6:55 am ]
Post subject:  Re: Eliminating "extra" steps in saving a file.

Then add the step to create that folder within the conditional.

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