-
I need to know how to list all folders in a folder or drive. I am making a list program that makes a list of all the files or folders in a dir, and converting them to proper case. Example:
C:\dos
C:\windows
C:\ati
will make a list like...
Dos
Windows
Ati
I already have the formatting and files done, but i have tried forever to get the folders and I just can't seem to get them to work, any suggestions.. thanks for your time!
- AZPC
ICQ: 25122538
E-mail: [email protected]
-
We'd use a file list box instead of trying to code for this yourself. It's faster and easier.
-
how?
how do i do that, and get each dir as text
-
Ok, I think I might understand now. Try this:
Sub ListDirs()
Dir1.Path = "c:\windows"
For i = -1 To Dir1.ListCount
Text1.Text = Text1.Text & Dir1.List(i) & vbCrLf
Next
End Sub
-
Drop a command button, two list boxes onto a form. Paste the following codes into the Declaration section and run it. I recommend you change the path to a smaller path than "C:\". If you leave the path as it is, it will list all folders on your computer.
When you click on the command button, it will take 2 to 3 seconds depending on the total number of folders you have for that directory.
Code:
Option Explicit
Private Sub Command1_Click()
ListSubDirs "C:\"
End Sub
Private Sub List1_Click()
Call ListFiles(List1.Text & "\")
End Sub
Private Sub ListFiles(strPath)
List2.Clear
On Error Resume Next
Dim strDir
strDir = Dir(strPath)
Do While strDir <> ""
If strDir <> "." And strDir <> ".." Then
List2.AddItem strDir
End If
strDir = Dir
Loop
End Sub
Private Sub ListSubDirs(Path)
On Error Resume Next
Dim intCount As Integer
Dim intX As Integer
Dim arrayV()
Dim strDir As String
strDir = Dir(Path, vbDirectory)
Do While strDir <> ""
'PURPOSE: Don't process parent or current directory entry so process it
If strDir <> "." And strDir <> ".." Then
If GetAttr(Path + strDir) = vbDirectory Then
' This is a directory
If (intCount Mod 10) = 0 Then
' Resize the array
ReDim Preserve arrayV(intCount + 10)
End If
intCount = intCount + 1
'PURPOSE: Add directory name to array
arrayV(intCount) = strDir
End If
End If
strDir = Dir
Loop
For intX = 1 To intCount
'PURPOSE: Directory's name in the list box.
List1.AddItem Path & arrayV(intX)
'PURPOSE: Recursive call
' Find this directory's sub-directories
ListSubDirs Path & arrayV(intX) & "\"
Next intX
DoEvents
End Sub
-
hey guys, instead of all this code why not check out the filesystemobject (FSO)
it lets you treat each folder like an individual collection with the ability to do a [for...each...next loop] to traverse the folder!
check it out
-
Then you can use Rename, Ucase and left to fix the names