I don’t mean to belabor the point and I offer this purely as a learning opportunity. If your code is working, that’s awesome! No need to change it. There are a couple of ways to approach this.
- The way (I assume) you are doing it now. Use FileAndFolderVerify simply to create the file if it doesn’t exist.
Var file as FolderItem
if FileAndFolderVerify("HC Prefs Folder 2", "Menu Prefs") = true then
file = SpecialFolder.Desktop.Child("HC Prefs Folder 2").Child("Menu Prefs")
TOS = TextOutputStream.Open(file)
TOS.WriteLine("97278888872")
TOS. close
end
Function FileAndFolderVerify(Folder as String, FileName as String) as Boolean
Var TOS As TextOutputStream
Var path As FolderItem = SpecialFolder.Desktop.Child (Folder)
If path.Exists = False Then path.CreateFolder
Var file As FolderItem = path.Child(FileName)
If Not file.Exists Then
TOS = TextOutputStream.Create(File) Rem Creates the file
End
If file.Exists then
Return True
else
Return False
End
End Function
Note that Create will open the file as TOS, but since it is a local variable, it will go out of scope and be destroyed when the function ends, closing the file again. You then reopen it in the calling code.
- Use FileAndFolderVerify to open the file in the TOS class property.
if FileAndFolderVerify("HC Prefs Folder 2", "Menu Prefs") = true then
TOS.WriteLine("97278888872")
TOS. close
end
Function FileAndFolderVerify(Folder as String, FileName as String) as Boolean
Var path As FolderItem = SpecialFolder.Desktop.Child (Folder)
If path.Exists = False Then path.CreateFolder
Var file As FolderItem = path.Child(FileName)
If Not file.Exists Then
TOS = TextOutputStream.Create(File) Rem Creates the file
else
TOS = TextOutputStream.Open(File)
End
If file.Exists then
Return True
else
Return False
End
End Function
- A little more advanced. Have FileAndFolderVerify return the TextOutputStream.
Var TOS as TextOutputStream
TOS = FileAndFolderVerify("HC Prefs Folder 2", "Menu Prefs")
if TOS <> Nil then
TOS.WriteLine("97278888872")
TOS. close
end
Function FileAndFolderVerify(Folder as String, FileName as String) as TextOutputStream
Var TOS As TextOutputStream
Var path As FolderItem = SpecialFolder.Desktop.Child (Folder)
If path.Exists = False Then path.CreateFolder
Var file As FolderItem = path.Child(FileName)
If Not file.Exists Then
TOS = TextOutputStream.Create(File) Rem Creates the file
else
TOS = TextOutputStream.Open(File)
End
Return TOS
End Function
Note that this code doesn’t use the class property at all. One valid reason to use the class property would be if you wanted to write to the file in multiple functions. In that case, use option #2. Otherwise, it would be preferable to dispense with the class property and just use local variables.