Creating a folder under SpecialFolder.ApplicationData

In all examples I’ve checked, the source-codes explain the saving process of prefs file as this:

[code]
dim f as folderItem

f = SpecialFolder.ApplicationData.child(MyPrefsFile)[/code]

This creates a file directly under “/Library/Application Support”,
But what is the syntax if I want to create a subfolder inside Application Support?
Especially if I want to name the folder as app’s bundle identifier: com.company.app

Try this:

[quote] dim f as FolderItem

f = SpecialFolder.ApplicationData.child(“MyFolderName”).child(MyPrefsFile)

if f.Exists then

else

f.CreateAsFolder

end if[/quote]

just do not forget to create the Folder go to the part [quote]f = SpecialFolder.ApplicationData.child (“MyFolderName”)[/quote] because the last part is your preference file.

Aha got it!
so in programming a child can have a child,
now isn’t it interesting? :smiley:

Another question. please look at this code:

[code] dim f as folderItem
dim t as textoutputStream

f = SpecialFolder.ApplicationData.child(“MyPrefFile”)
if f <> nil then
t = f.createtextFile
if t <> nil then
t.writeLine userName
t.writeLine userGender
if maritalStatus = true then
t.writeLine “true”
else
t.writeLine “false”
end if
t.writeLine offspringNumber
t.close
end if
end if[/code]

What exactly does “if f <> nil then” do here?
isn’t nil = something which doesn’t exist?
and if something is not nil, then it exists.
so why did the coder order a file which already exists to be created again?

Sorry I also forgot to ask:
the “.CreateTextFile” method is not recognized by Xojo autocomplete, but it is functioning well.
Is it a deprecated method?
And if so, what is the modern way to do it?

From what I know this code if f <> nil will make a folder be created regardless of whether or not there, so I use if f.exists then.

[quote=116982:@Radium Radiovich]Sorry I also forgot to ask:
the “.CreateTextFile” method is not recognized by Xojo autocomplete, but it is functioning well.
Is it a deprecated method?
And if so, what is the modern way to do it?[/quote]

That I can not answer that but it works fine until the last update, it would be nice if someone who knows can clarify this answer.

If F were to be NIL then it represents a folder path that CANNOT exist, not one that DOES not exit.

F can be NOT NIL and represent a legal path name, but it may be a path name that does not yet exist… this can be detected with IF NOT F.EXISTS

Look, I found my notes in the code below which I think is more current because it has Autocomplete, see if it works for you:

[quote] dim f as folderItem
dim t as textoutputStream

f = SpecialFolder.ApplicationData.child(“TEST1”)
if f <> nil then

t=TextOutputStream.Create (f)

end if[/quote]

t=TextOutputStream.Create (f) // < < < Just This Part

Dave S, Please can you tell me which is correct ?

t = f.createtextFile // This

t=TextOutputStream.Create (f) //Or this

I read the docs right now,

The docs recommends to use these:

t = TextOutputStream.Create(f)
t = TextInputStream.Open(f)

[quote=116986:@Dave S]If F were to be NIL then it represents a folder path that CANNOT exist, not one that DOES not exit.

F can be NOT NIL and represent a legal path name, but it may be a path name that does not yet exist… this can be detected with IF NOT F.EXISTS[/quote]
But doesn’t checking the boolean value of F.EXISTS answer both of them? rendering IF <> NIL unnecessary?

If you check Exists and f is nil, you will get a Nil exception and your program will go Kaboom !