Conditional Creation of Files and Folders (REALbasic getting started Mailinglist archive)
Back to the thread list
Previous thread: Mysterious Window Appearance
Next thread: DB Save As
| Conditional Creation of Files and Folders |
| Date: 29.05.04 00:27 (Fri, 28 May 2004 23:27:55 EDT) |
| From: GAmoore aol.com |
|
This is sort of related to my last question. I want to make a "Settings"
folder in the same locale as the application - if it doesn't exist create, if it does exist, then put files into it. I'm a little confused as to the create/connection distinction in this case. Similarly, with databases .... I read the examples for creating a new database or opening an existing one. However, how can you open it if it exists, create it if it does not exist? Here is my attempt : //____ Dim______________ Dim dbFile as FolderItem Dim db as REALdatabase //____ Setup______________ db sew REALdatabase dbFile hetFolderItem("DB File") db.DatabaseFileÛFile //____ Consolidated Connect/Create - 1st attempt _____________ If db.Connect() then //DB Opened ... proceed with operations //XX do stuff here else //Database NOT oened ... try to create If db.CreateDatabaseFile then //DB created, proceed //XX do stuff here else MsgBox "Database not found and not created" end if end if //...note the need to do the same stuff twice (upon succesful opening or succesful creation) //...and its not clear to me if you can do record inserts into the DB from a routine (method) //...- this would make it easier to modularize that operation, but how do you pass the db references? //____ Consolidated Connect/Create - 2nd attempt _____________ If not( db.Connect() then //DB Opened ... proceed with operations if not( db.CreateDatabaseFile) then MsgBox "Database not found and not created" return end if end if //do DB stuff here for both cases (connected to existing, or just created) - knowing that you // can't get here in bad cases (not created or not connected) - however this leaves me feeling // like there might be some case where the control can somehow sneak into this part of code _______________________________________________ Unsubscribe or switch delivery mode: <http://support.realsoftware.com/listmanager/> Search the archives of this list here: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Conditional Creation of Files and Folders |
| Date: 29.05.04 04:53 (Fri, 28 May 2004 22:53:28 -0500) |
| From: Jonathan Johnson |
|
On May 28, 2004, at 10:27 PM, <email address removed> wrote: > This is sort of related to my last question. I want to make a > "Settings" > folder in the same locale as the application - if it doesn't exist > create, if it > does exist, then put files into it. I'm a little confused as to the > create/connection distinction in this case. Similarly, with databases > .... I read the > examples for creating a new database or opening an existing one. > However, how can > you open it if it exists, create it if it does not exist? if dbFile.exists then // it exists else // it doesn't exist end if > //...- this would make it easier to modularize that operation, but > how do > you pass the db references? Declare a method with the parameter "db as Database". Just pass it then. > //do DB stuff here for both cases (connected to existing, or > just > created) - knowing that you > // can't get here in bad cases (not created or not > connected) - > however this leaves me feeling > // like there might be some case where the control can > somehow > sneak into this part of code If you ever have an error, feel free to use the "return" statement to leave the function. For example: if not db.Connect then MsgBox "Couldn't connect." return end if // go on your merry way If it couldn't connect, it won't ever get past that return statement. HTH, Jon |
| Re: Conditional Creation of Files and Folders |
| Date: 29.05.04 16:53 (Sat, 29 May 2004 11:53:51 -0400) |
| From: Charles Yeomans |
|
On May 28, 2004, at 11:27 PM, <email address removed> wrote: > This is sort of related to my last question. I want to make a > "Settings" > folder in the same locale as the application - if it doesn't exist > create, if it > does exist, then put files into it. I'm a little confused as to the > create/connection distinction in this case. Similarly, with databases > .... I read the > examples for creating a new database or opening an existing one. > However, how can > you open it if it exists, create it if it does not exist? Here is my > attempt > Creating a Settings folder is very simple. Here's what I would do. I'd define a function SettingsFolder() as FolderItem, probably as a function of the Application class, as follows. Function SettingsFolder() as FolderItem dim appFolder as FolderItem dim theSettingsFolder as FolderItem appFolder eetFolderItem("") If appFolder Is Nil or NOT appFolder.Exists then //something really odd has happened Return Nil End if theSettingsFolder {ppFolder.Child("Settings") If theSettingsFolder Is Nil then //you may have a permissions problem Return Nil End if If theSettingsFolder.Exists then If theSettingsFolder.Directory then Return theSettingsFolder Else //you could handle this case differently; for instance, you might want to alert the user Return Nil End if Else //try to create it theSettingsFolder.CreateAsFolder Return theSettingsFolder End if End Function Of course you'll still need to check the return value of this function when you call it, because it can fail, as the code above suggests. Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://support.realsoftware.com/listmanager/> Search the archives of this list here: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Conditional Creation of Files and Folders |
| Date: 29.05.04 14:10 (Sat, 29 May 2004 13:10:59 EDT) |
| From: GAmoore aol.com |
|
Thanks so much Charles. It works fine!
I put this code in the calling routine : ------------------------------------------------------------------------ dim theSettingsFolder as FolderItem theSettingsFolder eetSettingsFolder() if theSettingsFolder <> nil then //_____ CREATE FILE __________________ dlg.InitialDirectory 1heSettingsFolder dlg.promptTextonter a filename" dlg.SuggestedFileName/ata Export" + d.shortdate + ".txt" dlg.Title.ave exported class data as " fÐg.ShowModal() If f <> Nil then t o.createtextFile ..................... t.close And this (your method) : ------------------------------------------------------------------------------ - Function GetSettingsFolder dim appFolder as FolderItem dim theSettingsFolder as FolderItem appFolder tetFolderItem("") If appFolder Is Nil or NOT appFolder.Exists then //something really odd has happened Return Nil End if theSettingsFolder >ppFolder.Child("Settings") If theSettingsFolder Is Nil then //you may have a permissions problem Return Nil End if If theSettingsFolder.Exists then If theSettingsFolder.Directory then Return theSettingsFolder Else //you could handle this case differently; for instance, you might want to alert the user Return Nil End if Else //if it doesn't exist, then try to create it theSettingsFolder.CreateAsFolder Return theSettingsFolder End if _______________________________________________ Unsubscribe or switch delivery mode: <http://support.realsoftware.com/listmanager/> Search the archives of this list here: <http://support.realsoftware.com/listarchives/lists.html> |
Links
MBS REALbasic Plugins - Pfarrgemeinde Messdiener Nickenich