TextInputStream.Open() does not work unless GetOpenFolderItem is used

I’m trying to open and read from a text file.

f = GetOpenFolderItem(“text/plain”)

^-- works great BUT the code below does not work.

Can someone help me understand why the TextInputStream command fails? The file name is correct.

Dim f As FolderItem
Dim t As TextInputStream
//f = GetOpenFolderItem(“text/plain”)
f = GetFolderItem(“dbdir”)
If f<> Nil and f.Exists Then
Try
t = TextInputStream.Open(f)
Catch e As IOException
t.Close
MsgBox(“Error accessing file.”)
End Try
While Not t.EOF
PopupMenu1.AddRow(t.ReadLine)
Wend
End If

You need to include the extension as well as the file name, so “dbdir” is probably not the name of the file. Also, GetFolderItem doesn’t search the entire computer just the directory the app is contained in.

When you trace this code, at which line does it fail and how?

You need to specify the type of path:

f = GetFolderItem("dbdir", FolderItem.PathTypeNative)

I get an IOException.

The file is in the same directory as the application.

I figured it out - the following code seems to work great…

Dim f As FolderItem
Dim t As TextInputStream
//f = GetOpenFolderItem(“text/plain”)
f = GetFolderItem("").Parent.Child(“dbdir.txt”)
If f<> Nil and f.Exists Then
Try
t = TextInputStream.Open(f)
Catch e As IOException
t.Close
MsgBox(“Error accessing file.”)
End Try
While Not t.EOF
PopupMenu1.AddRow(t.ReadLine)
Wend
End If

I think that, even though the file name did not appear to have “.txt” on the end, that it was still required.

This should do:

f = GetFolderItem("dbdir.txt", FolderItem.PathTypeNative)

:slight_smile:

On PC you have a setting in the File Explorer option/View tab “Hide extension for known file types” that is checked as default. So txt is simply hidden, but mandatory for a folderitem.

On OS X, TextEdit tend to not ask for a .txt at the end of the file name in the save dialog (it adds it by itself if the CheckBox “Add txt as default extension is checked). :frowning:

On OS X there is a setting in the Finder’s Preferences dialogue: Show all filename extensions. The standard setting is Unchecked.

Windows had file extensions so that the OS knew what to do with a double clicked file.
The mac had resource forks, so it didn’t need them.

Windows started to hide the extension by default, to make it look like the mac.
On the mac they started using extension associations, and losing the resource forks.

Now both systems use extensions, and both systems try to hide them.
So test.csv and test. xls both look like the same file when they exist in a single folder!
I do’t agree that this makes like easier for the consumer. It certainly causes me some irritation on a regular basis, (and was the cause of a recent bug in my code that was testing the displayname of folder items instead of the actual name.)