Uploading folder file names

The code below is to upload a list of folders and files within a folder on my PC to a listbox named lstfiles.

Can anyone tell me why the file list does not appear in the selected folder view but does upload filenames to a listbox and how I can make filenames appear in the selected folder view and only upload the files I highlight.

// Event Handler: btnUploadFiles.Pressed
Var dlg As FolderItem
dlg = SelectFolder() // Opens a folder selection dialog

If dlg <> Nil And dlg.Directory Then
lstFiles.RemoveAllRows() // Clear the ListBox

// Iterate through the folder’s contents
For Each currentItem As FolderItem In dlg.Children
If currentItem <> Nil Then
If Not currentItem.Directory Then
lstFiles.AddRow(currentItem.Name) // Add only files
ElseIf currentItem.Directory Then
lstFiles.AddRow(currentItem.Name + “/”) // Add subdirectories
End If
End If
Next
Else
MessageBox(“No folder selected or the selection is not a folder.”)
End If

Just for clarity:

// Event Handler: btnUploadFiles.Pressed
Var dlg As FolderItem
dlg = SelectFolder() // Opens a folder selection dialog

If dlg <> Nil And dlg.Directory Then
  lstFiles.RemoveAllRows() // Clear the ListBox

  // Iterate through the folder’s contents
  For Each currentItem As FolderItem In dlg.Children
    If currentItem <> Nil Then
      If Not currentItem.Directory Then
        lstFiles.AddRow(currentItem.Name) // Add only files
      ElseIf currentItem.Directory Then
        lstFiles.AddRow(currentItem.Name + “/”) // Add subdirectories
      End If
    End If
  Next
Else
  MessageBox(“No folder selected or the selection is not a folder.”)
End If

I assume you’re using proper straight double-quotes and not the curly ones? What I’ve quoted seems to have the latter.

Please use the </> button when posting code.

2 Likes

Hi Tim
Yes, just using normal brackets. The code runs without errors, Just fails to show files in selection mode but does retrieve them.

What do you mean by “selection mode” ?

When the code is run, it shows the contents of my C: drive and I select the folder I want to use. At that stage it shows any sub folders but not the files in that folder, which I would like to see but when you say ok to the selection it actually retrieves all files in that folder even though it has not shown them. I would like to see the files at selection time and then only upload the ones I highlight.

The dialog returns just one item, namely a folderitem which is for the folder you selected. You then iterate over the children of that folderitem. Needless to say, thet then gives you all the children into the listbox. You’ll have to look in the doc to see if there’s a different dialog you can use that allows selection at dialog time, and then returns (somehow) a set of folderitems corresponding to the selection you made. Then you can iterate through that instead of the children of the one folderitem. I can’t remember if any such dialog exists, but I would have thought it should.

The code is iterating through the child items and building the list but that process is not displaying the item. What is required to make it display each item as it iterates through.

Well presumably the listbox is visible and is therefore displaying the list of the children. Otherwise, what does “display” mean?

i guess you expect a single folderitem without Children because you selected just a path?
in this case .Children is a method that read all sub folder or files.

https://documentation.xojo.com/api/deprecated/selectfolder.html
https://documentation.xojo.com/api/files/folderitem.html

To try and break the problem down I have reduced the code to

Var dlg As New SelectFolderDialog
dlg.Title = “Select a Folder to View Contents”

// Let the user select a folder
Var selectedFolder As FolderItem = dlg.ShowModal

This shows the folder but not any files in it.

A message appears in Explorer that “No items match your search”

This suggests that dlg.showModal does not supply a mask to search for. Can a mask of asterix.asterix be provided or do I need a different dlg option


What is the correct code to use in order to display a list of files in a folder

You should be looking at this:

https://documentation.xojo.com/api/user_interface/desktop/openfiledialog.html#openfiledialog-allowmultipleselections

Thankyou for your efforts Tim. The code works just as required> It lists the files and allows the use to select which files to upload.
The answer has been a real struggle to find,
What is the best way to find such answers in the documentation.

Ask yourself what is the item you’re working with this time - ah, openFileDialog - and then go look up that item, and read the Doc on that from end to end - properties, methods, and events. You likely won’t remember everything there this time, but enough may stick to trigger it next time.