Red de conocimiento informático - Problemas con los teléfonos móviles - Cómo leer todos los archivos y subcarpetas en un directorio con vb

Cómo leer todos los archivos y subcarpetas en un directorio con vb

Encuentra todos los archivos y subcarpetas en un directorio determinado

Pruébalo sin usar el objeto FileSystemObject y solo usa el código del control básico.

'El ejemplo requiere controles: Comando1, Lista1, Lista2, Archivo1, Dir1, todos usando propiedades predeterminadas.

'Por ejemplo, para buscar C:\, las palabras con '** se pueden modificar

Dim ctFind As Boolean

Private Sub Form_Load()

Me.Caption = "Buscar todos los archivos y carpetas"

Command1.Caption = "Buscar"

List2.Visible = False: File1.Visible = False: Dir1. Visible = Falso

Label1.Caption = "Listo"

Fin Sub

Sub privado Form_Unload(Cancelar como entero)

Fin

Finalizar sub

Sub privado Form_Resize()

Atenuar W siempre

En caso de error, reanudar siguiente

W = 720

Lista1.Move 0, 0, Me.ScaleWidth - W - 120, Me.ScaleHeight - 300

Comando1.Move Me.ScaleWidth - W - 60, 300 , W

Etiqueta1.Move 90, Me.ScaleHeight - 255, Screen.Width, 255

End Sub

Private Sub Command1_Click()

ctFind = No ctFind

Si ctFind Entonces

Command1.Caption = "Cancelar"

Llamar a FindDirFile("C:") '**Buscar C: Todos los archivos y directorios en \, o C:\Windows, etc.

Command1.Caption = "Buscar"

Else

Command1.Caption = "Buscar"

End If

End Sub

Private Sub FindDirFile(ByVal nPath As String)

Dim I As Long, nDir Como cadena, Ci como largo

ctFind = True

List1.Clear: List2.Clear

Si es correcto(nPath, 1) <> "\" Entonces nPath = nPath & "\"

Lista1.AddItem "Buscar" & nPath: List2.AddItem nPath

Archivo1.Pattern = "*"

Archivo1. Sistema = Verdadero: Archivo1.Hidden = Verdadero: Archivo1.ReadOnl

y = True

En caso de error, Ir a Cuo

Dir1.Path = nPath

En caso de error, Ir a 0

Hacer

Si List2.ListCount = 0, entonces salga de Do

nPath = List2.List(0)

List2.RemoveItem 0

Dir1.Path = nPath

Para I = 0 Para Dir1.ListCount - 1

GoSub ShowGe

nDir = Dir1.List(I)

Si es correcto( nDir, 1) <> "\" Entonces nDir = nDir & "\"

List1.AddItem "■" & nDir

List2.AddItem nDir

Siguiente

File1.Path = nPath

Para I = 0 Para File1.ListCount - 1

GoSub ShowGe

List1.AddItem " " & nPath & File1.List(I)

Siguiente

Bucle

Label1.Caption = "Búsqueda completada, ***encontrada" & List1.ListCount & " items"

ctFind = False

Salir de Sub

Cuo:

List1.AddItem "El directorio de inicio no existe: " & nPath

ctFind = False

Salir de Sub

ShowGe:

Ci = Ci + 1

Si Ci < 99, entonces regresa

Ci = 0

Label1.Caption = "Found" & List1.ListCount & ":" & nPath

DoEvents

Si ctFind entonces regresar

Fin Sub