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
// 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.
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.
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.
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.