Unable to debug this code

Worst: it display folders that ends with " - Data" on their names !

Back home because I cannot go ahead / must do somthing else.

TIA for your inputs.

Me.Icon = Create_a_ToC_Selected
Me.Refresh

Var f As FolderItem = FolderItem.ShowSelectFolderDialog()

If f Is Nil Then
  Return
End If

For Each file As FolderItem In f.Children
  
  If  file = Nil Then
    // Cancel ?
    System.DebugLog "Did you pressed the Cancel button ?"
    Return
  End If
  
  If Not file.IsFolder Then
    // Not a Folder ? Exclude it
    System.DebugLog "This file name is been excluded: " + file.Name
    
  ElseIf f.Name.EndsWith(" - Data") Then
    // An object whose name ends with " - Data" ?
    // Ignore these objects
    System.DebugLog "This file name ends with “ - Data” is been excluded: " + file.Name
    
  Else
    // Display the Folder Name
    LB.AddRow(file.Name.Split(" - "))
  End If
  
  // To avoid Infinite loop
  If UserCancelled Then Exit
Next

// Revert to normal
Me.Icon = Create_a_ToC

The idea is to create a list of Folder Names included in the selected Folder.

Current macOS / Xojo.

Not sure what you mean: Are you concerned about folders whose name ends with “ - Data” (so you need to actively exclude these), or is it that your f.Name.EndsWith(" - Data") test doesn’t catch these folders (possibly because it’s not a minus sign but a hyphen)?

And why are you concerned about infinite loops? It’s not like a folder could contain an infinite number of children. And is it really possible for f.Children to contain a nil FolderItem? I don’t think I’ve ever cared about testing for this possibility but maybe I’m not paranoid enough. To me a test file.Exists would make more sense as the folder could in principle have been deleted after the f.Children call.

You may want to exclude invisible folders btw. but this would depend on the precise task.

1 Like

Yes, I wanted to exclude any folder ending with " - Data".

unfortunately, sometimes I either do not look at the correct line(s) OR was OOM and unable to discover the error (OOM: Out of My Mind).

When I looked later, I realized why I could not exclude any Folder who Ends with " - Data":
The code does not works because I took the wrong FolderItem to make the test !

When I replaced f with file, that part worked as expected !

Conclusion: always use meaningful variables names !

The above line exclude files, incli-uding invisibles.

Testing UserCancelled is a simple caution line (just in case something wrong occured).

AT LAST: even if moving the code at debug time (arrow keys or MouseWheel), I discovers that the vertical bar is selectable (moveable) with the Mouse !

F is the parent folder. You want to check against file

Yes, that’s it. After many hours not looking at that code, when I came back to it, I SAW where the problem lie.

That also proves I have memory troubles.

Also, using two monitors with my laptop greatly help (but this cannot amways be done). THere are so many trouble makers (high temperature, human environment, health, age, etc.).

1 Like

Buy a rubber duck. When you have a problem like this one carefully explain to the rubber duck each line in the code and suddenly you will spot the problem. Funnily enough it even works for virtual rubber ducks.

A break from the machine is also worth while, as you discovered.

What also works, sometimes, is to write a question (here) until you nearly says all…

Sometimes this allows to found the solution / sometimes (worst), you gave the solution, but for unknown reason, you cannot see it (this happened to me last month or so): i gave the answer at the end of the question ! :frowning: