Traversing and checking folders/files Real 2012 v Xojo 2013r2

I have several folders inside user preferences that are specific to several different applications each having their own pref file.
The pref file structure contained inside this users preference folder is ‘Preferences:this apps name:this apps pref folder’.
In pre Xojo I used the following code segment to always check for folders on started

[code] Dim f As FolderItem
Dim newFolder as New FolderItem
Dim file_str As String
Dim myarray(-1) As String
Dim x As Integer

#If TargetMacOS Or TargetPowerPC Then

  myarray = File_Directory_Preferences.Split(":")
  
  For x = 0 to UBound(myarray)
    
    If x = 0 Then
      file_str = SpecialFolder.Preferences.AbsolutePath + myarray(x)
    Else
      file_str = file_str + ":" + myarray(x)
    End
    
    f = GetFolderItem(file_str)
    
    If Not f.Exists Then
      newFolder = f
      newFolder.createAsFolder
    End
  Next x
  
  f = New FolderItem(file_str + ":" + prefFileName) 'Check for f further down
  
#endif[/code]

The variable ‘File_Directory_Preferences’ contains the path name delimited using ‘:’ such as ‘first:second’ This method enables me to check many levels of folders and enables me to reuse this for non pref files by plugging in other variables.

In Xojo AbsolutePath is deprecated and using NativePath as suggested creates a error. What am I doing wrong? Suggestions welcomed. Thanks

Please don’t do that kind of string operations with paths.
Please use folderitem.child and parent to go up and down.

Const subFol1 = “My Company”
Const subFol2 = “My App”

//#If TargetMacOS Or TargetPowerPC Then
//Not needed because this code is now xplat

dim f As FolderItem = SpecialFolder.Preferences

if f <> Nil Then
f = f.Child(subFol1)
if f = Nil Then Return
//add code to handle f = Nil

if not f.Exists Then
  f.CreateAsFolder
end if

f = f.Child(subFol2)
if f = Nil Then Return

if not f.Exists Then
  f.CreateAsFolder
end if

else
MsgBox “The standard Preferences folder does not exist”
end if

Thanks for your reply and the mention of xplay.

Your suggestion works as a static way of doing this. I have the full Child path inside a string variable using a delimiter for each Child. This allows you to have a variable Child path and check/create as needed using a global string

Here is where I am at integrating your suggestion:

Defined Globally:
Dim my_Childs_folders, child_str As String = “First Child:Second Child:Third Child”

[code]
Dim myarray(-1) As String
Dim x As Integer
Dim f1 As FolderItem = SpecialFolder.Preferences

myarray = my_Childs_folders.Split(":")

For x = 0 to Ubound(myarray)
  If x = 0 Then 
    child_str = myarray(x)
  Else
    'Add "/" indicated in Language Reference FolderItem.Constructor(path as String, PathType as Integer=PathTypeAbsolute)
    child_str = child_str + "/" + myarray(x)
  End
  f1 = f1.Child(child_str)
  
  if not f1.Exists Then
    f1.CreateAsFolder
  end if
  
Next x[/code]

I currently have all folders existing under User Preferences. When you run this first time around things are good. Second time around the folder is not found even though it exists. If you substitute a ‘:’ you get an error. So’/’ is correct as the Language Reference indicates however the second path indicates the folder does not exist when it does.

Ok, just figured it out.

Changed the code to the following and all is good. Now this works to any depth defined in the variable using the delimiter

Dim my_Childs_folders, child_str As String = “First Child:Second Child:Third Child”

[code]
myarray = my_Childs_folders.Split(":")

  For x = 0 to Ubound(myarray)
    
    f = f.Child(myarray(x))
    
    if not f.Exists Then
      f.CreateAsFolder
    end if
  Next x
  
  f = f.Child(prefFileName) ' Check later to see if this exists
  [/code]

Thanks for everyones feedback

Your insistence on storing Your path as a colon delimited string is prone to errors and is not Xplat. We tried to show you a better way. Good luck.

Roger… actually in his last post, he does do it a more appropirate way. By splitting his string and using CHILD on each array item

Dave - thanks for checking. I can sometimes miss it even though I thought it. I checked this on MAC 10.5, 10.7 and on Windows XP, 7 and this functions correctly without any #Ifs.

Thanks again for everyones input