TextOutputStream error

Hi

I’m trying to write multiple files at once.
First file is done, but fails trying second file…

		dim cdataf as FolderItem = SpecialFolder.Desktop
		cdataf = cdataf.child("test")
		if not cdataf.Exists then cdataf.CreateAsFolder
		
		Dim value As String = "yksi,kaksi,kolme"
		
		Dim i As Integer
		Dim test() As String = Split(value, ",")
		For i = 0 To test.Ubound
				cdataf = cdataf.child(Str(i) + ".txt")
				dim cdatat as TextOutputStream = TextOutputStream.Create(cdataf) //Here it fails on second file
				cdatat.Write(test(i))
				cdatat.Close
		Next i

What am I doing wrong and how to solve this?

Thanks

Jukka

What kind of failure are you getting?

IOException with Error -1407

you are creating deeper and deeper folders as it runs

cdataf = cdataf.child(“test”)
then
test/1.txt
then you attempt to create
test/1.txt/2.txt … which will fail because 1.txt is not a directory

This line is likely the culprit
cdataf = cdataf.child(Str(i) + “.txt”)

it make cdataF refer to a FILE and so the first time through it works but the next time cdataf refers to a FILE and you try to get its child

dim cdataf as FolderItem = SpecialFolder.Desktop
cdataf = cdataf.child("test")
if not cdataf.Exists then cdataf.CreateAsFolder
		
Dim value As String = "yksi,kaksi,kolme"
		
Dim i As Integer
Dim test() As String = Split(value, ",")
For i = 0 To test.Ubound
	dim dataF as Folderitem = cdataf.child(Str(i) + ".txt") // <<< a temporary so we dont alter cDataF
	dim cdatat as TextOutputStream = TextOutputStream.Create(dataF) //Here it fails on second file
	cdatat.Write(test(i))
	cdatat.Close
Next i

Oh, so simple :slight_smile:
And I fight with this looking totally wrong place :confused:

Thanks!

Jukka

single stepping in the debugger would have revealed what was wrong since you know it did one file right and not the second
checking the variables inside the loop would have shown you that

Yes, I can see it now. And it’s there so many times (Absoluth-, Native-, Shell- and UrlPath) :slight_smile:
(but I was too sure that TextOutputStream was quilty, trying ti Dim it in different scope, close and make Nil different place etc.)

Usually, I create a master FolderItem that refer to the folder where I want to add items, the in the Loop, I use a Child ofthis master folder and use it to save files. Because I Dim it in the loop, I do not get troubles (duplicate files for example); I also put a SQLiteDateTime in the file name when I cannot use the Loop indice or something else to make the file name unique.

This is the same answer Norman gaves, excepted here yu will write in your folder instead of inside a SpecialFolder. You can combinate both answers: use SpecialFolder to create your Master folder, then…

At last, when I use the loop indice in the file name (to get unique file names), I use Format(i,"#,###") to get a better looking file name.