'---------------------------------------------------
'Agregar lña referencia a Microsoft Scripting Runtime
'---------------------------------------------------
Private Sub Command1_Click()
On Error GoTo errsub
Dim Fso As FileSystemObject
Dim El_Directorio As Folder
Screen.MousePointer = vbHourglass
DoEvents
List1.Clear
Set Fso = New FileSystemObject
Set El_Directorio = Fso.GetFolder(Trim$(Text1))
List1.AddItem Trim$(Text1)
' Comienza a listar las carpetas
Call Listar_Directorios(El_Directorio)
Screen.MousePointer = vbDefault
'Error
Exit Sub
errsub:
MsgBox Err.Description, vbCritical
Screen.MousePointer = vbDefault
End Sub
Private Sub Listar_Directorios(ByVal El_Directorio As Folder)
On Error GoTo errsub
' Variable de tipo Folder
Dim Subdirectorio As Folder
' Recorre los subdirectorios
For Each Subdirectorio In El_Directorio.SubFolders
'Agrega el path
List1.AddItem El_Directorio.Path & "\" & Subdirectorio.Name
'sigue listando los directorios
Listar_Directorios Subdirectorio
Next
Exit Sub
'Error
errsub:
'Error de permiso denegado
If Err.Number = 70 Then
Resume Next
ElseIf Err.Number = 91 Then
Screen.MousePointer = vbDefault
Exit Sub
Else
MsgBox Err.Description, vbCritical
Exit Sub
End If
End Sub
Private Sub Form_Load()
Command1.Caption = " Listar "
Text1 = "c:\a"
End Sub
Private Sub List1_Click()
'Variable de tipo FILE y FOLDER para listar los archivos de un path
Dim El_Archivo As File
Dim El_Directorio As Folder
'Si no hay items en el List sale
If List1.ListIndex = -1 Then Exit Sub
List2.Clear
'Nuevo objeto FileSystemObject
Set Fso = New FileSystemObject
' Obtiene el directorio
Set El_Directorio = Fso.GetFolder(List1.List(List1.ListIndex))
' Lista los ficheros de esta carpeta
For Each El_Archivo In El_Directorio.Files
'Añade la ruta
List2.AddItem El_Archivo.Name
Next El_Archivo
End Sub