Listing files in a listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to be able to open a folder and list all the subfolders in one
listbox. Then I would like to be able to select a folder from that listbox
and display all the files in another listbox
 
Hi

You are familiar with

Sub test()
Dim F As Variant
F = Application.GetOpenFilename
If Not F = False Then MsgBox CStr(F)
End Sub

?

What you request is possible, but how will you know where the starting
folder is and that it's always two levels above where the file is ?

HTH. Best wishes Harald
 
Harald,
What I am doing is this. I have a main folder on a server. In the main
folder I have about 25 sub folders. I want to be able to list all these
folders in a listbox. I want to do this in the code so that any time a new
folder is added it will automatically appear in the listbox.
 
Once the VB editor is open, go to 'Tools > References' and set a reference
to "Microsoft Scripting Runtime"

I tested using a top folder of "C:\Test", you will need to change to the
path of the folder on your server. This does not deal with subfolders of
subfolders since it didn't sound like you would have that situation. I have
a UserForm1 with ListBox1 and ListBox2. When the form is activated,
ListBox1 is populated with a blank line and then the list of all subfolders
of C:\Test. When one of the subfolders is selected ListBox2 is cleared and
then populated with a single blank line followed by the list of files
contained in the selected subfolder.

Dim FSO As Scripting.FileSystemObject
Dim topFldr As Scripting.Folder
Dim someSub As Scripting.Folder
Dim workFldr As Scripting.Folder
Dim someFile As Scripting.File


Private Sub ListBox1_AfterUpdate()
Set workFldr = FSO.GetFolder("C:\Test\" & ListBox1.Value)
ListBox2.Clear
ListBox2.AddItem " "
For Each someFile In workFldr.Files
ListBox2.AddItem someFile.Name
Next someFile

End Sub


Private Sub UserForm_Activate()
Set FSO = New Scripting.FileSystemObject
Set topFldr = FSO.GetFolder("C:\Test")
ListBox1.AddItem " "
For Each someSub In topFldr.SubFolders
ListBox1.AddItem someSub.Name
Next someSub

End Sub

Private Sub UserForm_Deactivate()
Set FSO = Nothing
End Sub


Steve
 
I built a program which would take a given directory, list all
sub-directories and files in them to a spread sheet, allow you to modify the
names, attributes, and/or directories for all files, and go back through the
list to re-name, move, and alter attributes to the files. Came out pretty
handy. I used a recurisve method to push new directories encountered onto a
stack and when the time came, pop them off and treat them like the
originally selected directory. (Recursion was controlled by the user, so it
didn't confuse the user too much. He could work with just one directory or
the whole shebang.)

But I'm not really that advanced in VBA. I just used simple file
manipulation techniques:
ChDrive to select the disk
ChrDir to select the directory
Name to change a files name and/or the directory in which it resides
SetAttr to change attributes
But most importantly, the Dir function to get files listed in a directory.

Could look something like this ...
fnm = Dir("d:\pathnameyouwanttofindfilesanddirectoriesin", vbNormal)
do until fnm = "" 'No more files in that directory
Put 'er in the listbox using .Additem
if fnm is a directory, remember this for later processing
fnm = Dir 'Get the next file name in the directory
loop

If this isn't totally off the wall, I'd be happy to expound on this type of
program for you and give you more specific hints.

-Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top