Creating a New Folder and same named Filename within

I’ve spent many hours researching this, I’ve gleaned some info from similar situations, but still can’t work it out.

What I need, is that the user will input a filename in an input box, that name will be used to create a folder with that name, and also create a filename with the same name within that folder.

No problem creating the folder from user input, but when trying to create a file with the same name (or any other) within that folder, I get a NilObjectException error.

[code] Dim f As FolderItem
Dim fn As String = “folderAndFilename”

f = SpecialFolder.ApplicationData.Child (fn)
f.CreateAsFolder

//

Dim fl as FolderItem
Dim myFileName As TextOutputStream
myFileName = TextOutputStream.Create(fl)
myFileName.Write (“test data”)
myFileName.Close[/code]

I know I’ve got the last part wrong. Any pointers much appreciated.

Cheers.

Dim fl as FolderItem

but you never tell Xojo what file fl refers to.

fl = f.child(fn) 

Well, thanks Jeff - I got it to work with that extra bit of missing code. This was a big step with my software going forward.

The thing that confuses me is the whole file I/O scenario with Xojo: Dim fl as “FileNameItem” would make more sense than “FolderItem”. Creating a folder or filename is too similar.

Someone should write a book or at least a good chapter on file I/O operations.

Nevertheless, my code now works perfectly:

[code] Dim f As FolderItem
Dim fn As String = txtMotorFileName.text

f = SpecialFolder.ApplicationData.Child (fn)
f.CreateAsFolder
//
Dim fl as FolderItem
fl = f.child(fn + “.original”)
Dim myFileName As TextOutputStream
myFileName = TextOutputStream.Create(fl)
myFileName.Write (“test as text data is here . . . bla bla bla”)
myFileName.Close[/code]

The user can input a name via a text field (txtMotorFileName.text) and a folder is created, and a filename with same name, plus the extension (.original). Perfect (except for error checking to be done)

I don’t completely understand why or how it works, but it does.

So Jeff, “wham bam thank you ma’am”. Time to move forward. Cheers :slight_smile:

Im sure you confused yourself by having the names be the same.

Consider how much easier it is to follow when the names are different:

[code] Dim fold as FolderItem
Dim fil as FolderItem

fold = SpecialFolder.ApplicationData.Child (“Foldername”)
fold.CreateAsFolder

fil = fold.child(“Filename”)
myFileName = TextOutputStream.Create(fil)[/code]

Thanks Jeff, yes that does may things much much clearer.