Read text files

The passed FolderItem to a Read_Text Function is Nil or does not exists.

So I add a call to the Function from the main window to a Text file in the project folder: the Function returns the txt file contents as requested.

“Direct” call:

Dim f as New FolderItem("List of Stories.txt")

TA.Text = f.NativePath + EndOfLine

TA.AddText Read_Text(f)

In this case macOS ask me for the permission to read, not with the code below.

Call in a method, different window:

Dim f As New FolderItem("Sunday Area")
f = f.Child("Contents")
f = f.Child("14PM the Mysterious Maiden”")
f = f.Child("Rules of the game.txt ")

TA.Text = f.NativePath + EndOfLine

TA.AddText Read_Text(f)

REMEMBER: this is bulk code, used for debug purpose.

i would start folderitem with a valid path from specialfolder.

In debug mode, that wont be the same location as it is in release mode.
Without a qualified parent, this path is relative to the executable.

Start with

Dim f As FolderItem
f = specialfolder.applicationdata.child("com.mycompany.myapp")
//this folder should be created at app startup.
//it will exist with the same path, whether you are debugging or release
//then
f = f.Child("Sunday Area")
//etc

Yes, Man, I am a stupid new user.

Hi Jeff,

that is what I forget to say: debug mode. I am in the early stage and have only a basic data structured folder that I put in the project folder (for testings, of course).
That is the reason why I used that way to build a Reference to a text file.
I started earlier this afternoon to add References (FolderItem) to the above folders (they are needed in other windows).

It is still strange that a valid FolderItem (I checked in the debuger) is not allowed to read a text file.

LBN: I really have a problem and it certainly is in the SSD. Once I’ve removed that trouble, I will resume operations.
The FolderItem that referenced a text file some hours ago, do not do it anymore :frowning:

Is the SSD an External drive?
You may not be getting permission to read from that if your debug app is being sandboxed.
But
Dim f As New FolderItem(“Sunday Area”)

wouldnt be looking at an external drive by default, unless your debug app was there too…

:slight_smile: caricature

1 Like

As incredible as it seems, and after I followed Jeff advice, the problem still exists.
Worst: I created a brand new folder, set it to a conventional name (no fancey characrer) and the problem is still here.
In my last attempt, I add more info in the MessageBox text and:
The FolderItem is not Nil
The FolderItem does not Exists
The FolderItelm.isReadable says False.

It may be time to do something else.

I created a brand new folder,

you created a folder manually?

at windows i used this method to create a data folder.

Public Function Path() As FolderItem
  System.DebugLog CurrentMethodName
  
  Var path As FolderItem = SpecialFolder.ApplicationData.Child("MyFolder")
  If path.Exists = False Then path.CreateFolder
  
  Return path
End Function

So what are you expecting?
if the folder item doesnt exist, how can you read it?

Have you considered adding a file select dialog and choosing the file manually?
That will tell you if the problem lies in the folderitem, access permissions, or more likely the way you are creating the path to it in code.

1 Like

Sorry Jeff, but you misunderstand what I wrote: the just added folder appears in code as FolderItemX.Exists = False.

The text file I want to read is stored in that folder (directory).

The text file I want to read is stored in that folder (directory).

Is it though?
Have you tried adding a file select dialog and choosing the file manually?

No, I will do it in minutes (when back) I have to go.

As I suspected, I can not only see the folder, but choose a .txt file that was inside, using the Read_Text Function that do not works where I need it to load the text file.
In the main window, I only add in .MouseDown:

Dim f As FolderItem

'f = FolderItem.ShowSelectFolderDialog() // 1st test
f = FolderItem.ShowOpenFileDialog("raw-text")

TA.Text = f.NativePath + EndOfLine

TA.AddText Read_Text(f)

The mystery thickens by the hour!…

I still think it’s permissions.

Testing here, I took your mousedown code, and added my own guess at what your Read_text method looks like:


//Function Read_text(f as folderitem)  as string
dim tis as textinputstream
dim line as string
try
  tis = TextInputStream.Open(f)
  line = tis.ReadAll 
  tis.close
catch 
  //??
end try
if tis <> nil then tis = nil

return line

No problem opening text files on my local hard drive, and even in iCloud.
If I try to open a text file that is in another partition that I use for Ventura, I get error 60 on the ReadAll line, which I would expect as I don’t have permission to read there.
Is that what you get?

No, the last check (yesterday late evening) was:


If Story_Contents_FI = Nil Then
  // Not Nil
If Not Story_Contents_FI.Exists Then
  // Does not exists
If Not Story_Contents_FIisReadable Then
  // Quit on the previous If block…

The Read_Text works fine with:

Dim f As FolderItem

f = FolderItem.ShowOpenFileDialog("raw-text")

TA.AddText Read_Text(f)

I used to choose the Folder first without trouble, then changed to read the text file and that worked fine.

As I wrote earlier, the mystery…

BTW: the code in Read_Text looks like what you shared (comes from the LR, changed file Reference name.

So ShowOpenFileDialog works?
It was hard to understand that from your post.

But this lot fails:

If Story_Contents_FI = Nil Then
  // Not Nil
If Not Story_Contents_FI.Exists Then
  // Does not exists
If Not Story_Contents_FIisReadable Then

You realise that this only checks whether story_contents _FI exists if it is NIL?
If the file is NIL, it won’t exist…

Look at this:

dim MyFolder as folderitem = specialfolder.applicationdata.child("myfolder")
//this should not be needed, but just in case
if MyFolder.exists = false then  MyFolder.CreateFolder
//of course, if we had to create this folder, there wont be anything inside it!

dim Story_Contents_FI as folderitem
dim f as folderitem
dim bOKToProceed as boolean = true
try
Story_Contents_FI = MyFolder.child("Sunday Area")
f = f.Child("Contents")
f = f.Child("14PM the Mysterious Maiden”")
Story_Contents_FI = f.Child("Rules of the game.txt ")
catch
bOKToProceed = false
msgbox "One of those children doesnt exist, but you should check as you go.."
end try

//lets check the final file
if bOKToProceed then
If Story_Contents_FI <> Nil   and  Story_Contents_FI.Exists  Then
TA.Text = Story_Contents_FI.NativePath + EndOfLine
TA.AddText Read_Text(Story_Contents_FI)
else
 msgbox "The file is not there"
end if
else
msgbox "Skipped the reading part due to an error in the parent folders"
end if
1 Like

You may try “MessageBox File.NativePath” to debug when your file doesn’t exist, just to make sure it’s what you expect. When constructing the folderitem fails, the folderitem you have won’t exist and can point to “weird” locations (like /private/var/folders/q2/zrgjn7v57zv2msfrs673jbdh0000gn/T/‘/Applications/DT10.64.app’ which happens if your construction path has quotation marks around the original path; here, File=new FolderItem(‘/Applications/DT10.64.app’) results in a path resolved mistakenly in the temporary folder).
Before trying to speculate on why you can’t read the file, I’d highly suggest making sure the path is what you expect.

1 Like

I used System.DebugLog File.NativePath

If you provide a sample project, I’m sure that you will get the help needed to fix your problem.