New topic: 

FolderItem question

<http://forums.realsoftware.com/viewtopic.php?t=31359>

       Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic         Author  
Message       serd83           Post subject: FolderItem questionPosted: Tue Dec 
01, 2009 8:15 pm                        
Joined: Thu Feb 22, 2007 7:08 pm
Posts: 767              Assume there is a folder in the applications order with 
the name "Main". In this "Main" folder are again a order with the name "Order 
2", and in this are some files, etc.
When I want check a file for nil, but the "Main" was renamed or deleted, so I 
become for this a nil:
Code:dim f as folderitem = 
specialfolder.applications.child("Main").child("Order 2").child("aFile.txt")
The "only way" I found is to check before if the order exist. Or miss I a 
function for this?
Code:dim f as folderitem
dim par as folderitem = specialfolder.applications.child("Main").child("Order 
2")
if par <> nil and par.exists = true then f = 
specialfolder.applications.child("Main").child("Order 2").child("aFile.txt")
   
                            Top               JShaffer           Post subject: 
Re: FolderItem questionPosted: Tue Dec 01, 2009 8:46 pm                        
Joined: Fri Jan 23, 2009 9:18 am
Posts: 18
Location: Summerfield, Florida              I saved this code snippet that 
might be of use.
 
I'm not sure of whom to credit for the code.

  // Set up folder path
  dim fPath() as string = Array("Programming Data","Folder 1","Folder 2")
  
  // Base FolderItem
  dim f as FolderItem = SpecialFolder.Documents
  
  // Search for each folder
  for each fName as string in fPath
  f = f.Child(fName)
  if f = nil or not f.Exists then
  f.CreateAsFolder // will add the missing folders
  // or error code 
  end
  next

HTH
Jim   
                            Top                timhare           Post subject: 
Re: FolderItem questionPosted: Wed Dec 02, 2009 1:17 am                        
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 6882
Location: Portland, OR  USA              Quote:The "only way" I found is to 
check before if the order exist. Or miss I a function for this?
That is the only way.  Any time you chain more than 2 folderitems together like 
that, you are susceptible to a nilobject exception.  If there is ever any 
question, you should handle each node in the path one at a time.   
                            Top               applesource           Post 
subject: Re: FolderItem questionPosted: Wed Dec 02, 2009 6:23 am                
               
Joined: Thu Aug 06, 2009 2:25 pm
Posts: 214
Location: Oregon              Is the proper way, then, to build a folderitem 
one folder at a time and check for nil each time, like this?

Code:dim f as folderitem
f=SpecialFolder.Preferences.child("My App")
if not (f is nil) and f.exists then f=f.child("Second Folder")
if not (f is nil) and f.exists then f=f.child("Third Folder")

// which leaves us with f pointing to "preferencesfolder/my app/second 
folder/third folder"


Is this the correct technique?     
_________________
AppleSource Software
http://www.applesource.biz  
                            Top                serd83           Post subject: 
Re: FolderItem questionPosted: Wed Dec 02, 2009 1:44 pm                        
Joined: Thu Feb 22, 2007 7:08 pm
Posts: 767              Thanks for the answers. As you see you can use 
different techniques for this. Important is that you should be careful when you 
chain more than 2 folderitems together, as Tim said.
It is logical, but it can happen that you want do anything with a file and use:
Code:dim f as folderitem = 
specialfolder.applications.child("Main").child("Folder 2").child("aFile.txt")
if f <> nil and f.exists = true then f.launch

But it can happen that the user change for example the "Main" folder name and 
you become a nil for this:
Code:dim f as folderitem = 
specialfolder.applications.child("Main").child("Folder 2").child("aFile.txt")   
                            Top               npalardy           Post subject: 
Re: FolderItem questionPosted: Wed Dec 02, 2009 1:55 pm                        
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 5455
Location: Canada, Alberta, Near Red Deer              serd83 wrote:Code:dim f 
as folderitem = specialfolder.applications.child("Main").child("Folder 
2").child("aFile.txt")

The problem here is 
 1) ifapplications folder is missing (it can happen) then everything after that 
is not going to work and you will get a nil object exception
 2) if main is missing then everything after that is not going to work and you 
will get a nil object exception
 3) if Folder 2 is missing then everything after that is not going to work and 
you will get a nil object exception

you can wrap it in a nil object exception handler (see try ... catch)
or other error checking as appropriate     
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]     
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to