Folderitem does not exist. What am I missing?

Hello all. I have not had to create folderitems until now, so I am definitely missing experience with this. I have a small piece of method that creates one. The folderitem is created (not nil) but the test for folderitem.exists fails. What am I missing?

Var F as folderitem
Var fdest as string = App.ExecutableFile.Parent.NativePath + "UploadedDocuments\"  + FN
F = New folderitem (FDest, FolderItem.PathModes.Native)

If F <> Nil and F.exists Then   
  t = TextOutputStream.Open(F)
  t.Write(outputstr)
  t.Close
End If

Here, FN is equal to “test.pdf”. The folderitem is not nil, I can seee all its properties in the debug pane. If I only test for F<> Nil, I get an IOException 3 (“path not found” on Windows). If I test as shown, I just skip the writing part. What am I missing that causes F to not exist?

This is being developed on Windows 11, 2024 R3.

Thanks in advance for your help!

Does the file / folder that the FolderItem references exist on the disk?

yes. The path does exist.

The folderitem exists, the file does not. Having the former does not cause a file/folder to spring into existence, you have to create it.

What is at the path, a file or a folder and does the application have permission to access it?

The path is S:\Software\Geoff\2024 R3\UploadedDocuments. It exists and is open to write for all users. The user running the program has admin rights too.

Create the folderitem. That makes total sense. I did not find anything like folderitem,create. How should I do this?

For me:

App.ExecutableFile.Parent.NativePath

has no slash (or backslash) in the end…

Try with

App.ExecutableFile.Parent.NativePath + "\UploadedDocuments\"

maybe?

But what do I know about Windows programming (o;

Are you trying to append to an already-existing file or create a totally new one?

I tried that originally. app.executablefile.parent.nativepath does show "" for me on Windows 11. I had double \ before uploadeddocuments when I tried that.

I am creating a new file. The folder is totally empty.

Then F.Exists is going to be False, is it not?

Edit: F references the file you haven’t written yet.

correct. If I don’t test for that, then the outputstream fails with ioexception 3.

When I start a new project involving file access I always keep a small DesktopTextArea handy where I log certain things with automatic scroling…

ConsoleArea.AddText(s)

var amountOfLineNumbers as integer = ConsoleArea.LineNumber( ConsoleArea.Text.Length )

for i as integer = 0 to amountOfLineNumbers
  ConsoleArea.VerticalScrollPosition = i
next

Can you set a Label text to “fdest” ?
Then you see where it tries to access the file…

You should try to avoid converting FolderItems to path strings, manipulating them and then converting back to FolderItems.

This should work better:

Var F as folderitem
F = App.ExecutableFile

If F <> Nil Then
  F = F.Parent

   If F <> Nil Then
     F = F.Child("UploadedDocuments")

      If F <> Nil Then
        F = F.Child(FN)
      End If
   End If
End If

If F <> Nil Then   
  t = TextOutputStream.Open(F)
  t.Write(outputstr)
  t.Close
End If

(the above code that builds F could be simplified if you used exceptions).

OK then your path is wrong or inaccessible. Your need to use system.debuglog to print it out and check that it’s correct.

Or use the debugger; that’s what it’s for.

This is how I had initially, which failed in the same way. Maybe I made a mistake in my original code. I will shamelessly copy yours and try it.

I have been using the debugger. The path in the debug pane is correct.

So you need to remove the F.Exists and then see why the I/O error occurs. Is that happening on the Open or the Write ?

I modified slightly your suggested code:

Var F as folderitem
'Var fdest as string = App.ExecutableFile.Parent.NativePath + "UploadedDocuments\"  + FN
'F = New folderitem (FDest, FolderItem.PathModes.Native)
F = App.ExecutableFile.Parent
If F <> Nil Then
  F = F.Child("UploadedDocuments")
  
  If F <> Nil Then
    F = F.Child(FN)
  End If
End If


If F <> Nil and F.exists Then   
  t = TextOutputStream.Open(F)
  t.Write(outputstr)
  t.Close
End If

F still does not exist.