Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[VB] - Recorrer todos los archivos y directorios recursivamente

545 views
Skip to first unread message

Eduardo

unread,
Jun 25, 2001, 11:27:08 AM6/25/01
to
Hola:
Alguinen conoce o ha realizado una aplicación para recorrer todos los
archivos y directorios de los discos, pero sin usar los controles FileList
ni DirList ni nada de eso?. Además, será posible hacerla de forma
recursiva?. Desde ya, muchas gracias.


Iván Vega Rivera

unread,
Jun 25, 2001, 12:28:28 PM6/25/01
to
Agregas una referencia a Microsoft Scripting Runtime.

Después recorres la estructura de carpetas/archivos así:

Dim fsoUtil As FileSystemObject
Dim fldRoot As Folder
Dim drvChild As Drive, fldChild As Folder, filChild As File ' Unidad,
carpeta, archivo

Set fsoUtil = New FileSystemObject
Set fldRoot = fsoUtil.GetFolder("C:\") ' Cambias esto para cada unidad

For Each drvChild In fsoUtil.Drives
' Haces lo que quieras con drvChild
For Each fldChild In fldRoot
' Haces lo que quieras con fldChild
For Each filChild In fldChild
' Haces lo que quieras con fldChild
Next
Next
Next

"Eduardo" <ebar...@coto.com.ar> escribió en el mensaje
news:e04FcqY$AHA.1316@tkmsftngp04...

Eduardo

unread,
Jun 25, 2001, 3:25:39 PM6/25/01
to
Iván, copié el ejemplo que me diste y no me funciona. Esto sirve para
recorrer todos los archivos y subdirectorios de c:\?

"Iván Vega Rivera" <y...@better.ask> escribió en el mensaje
news:#06zYMZ$AHA.2028@tkmsftngp04...

Norman A. Armas

unread,
Jun 25, 2001, 4:46:26 PM6/25/01
to
HOWTO: Recursively Search Directories Using FileSystemObject

Q185601


The information in this article applies to:

Microsoft Visual Basic Learning, Professional, and Enterprise Editions for
Windows, versions 5.0, 6.0


SUMMARY
The FileSystemObject class can be used to recursively search directories and
find files. This article demonstrates the use of FileSystemObject to search
for specific files.


MORE INFORMATION
The FileSystemObject class is found in the Scrrun.dll file, which can be
obtained by installing any one of the following packages:

Windows Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
Scripting 3.1 upgrade
Visual Studio 98
Visual Basic 6.0

The FileSystemObject class gives better performance than using such Visual
Basic intrinsic functions as Dir and GetAttr, and is much simpler to
implement.

Example
Start a new Standard EXE project in Visual Basic. Form1 is created by
default.


On the Project menu, click References, and add a reference to the Microsoft
Scripting Runtime. If this option is not listed, browse for Scrrun.dll on
your system. Install one of the tools previously listed if necessary.


Add a CommandButton, a Label, and a ListBox to Form1. Adjust the width of
the Label to the width of the form.


Paste the following code in the General Declarations section of Form1:

Option Explicit
Dim fso As New FileSystemObject
Dim fld As Folder

Private Sub Command1_Click()
Dim nDirs As Integer, nFiles As Integer, lSize As Long
Dim sDir As String, sSrchString As String
sDir = InputBox("Please enter the directory to search", _
"FileSystemObjects example", "C:\")
sSrchString = InputBox("Please enter the file name to search", _
"FileSystemObjects example", "vb.ini")
MousePointer = vbHourglass
Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
MousePointer = vbDefault
MsgBox Str(nFiles) & " files found in" & Str(nDirs) & _
" directories", vbInformation
MsgBox "Total Size = " & lSize & " bytes"
End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, _
nDirs As Integer, nFiles As Integer) As Long
Dim tFld As Folder, tFil As File, FileName As String

Set fld = fso.GetFolder(sFol)
FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
vbHidden Or vbSystem Or vbReadOnly)
While Len(FileName) <> 0
FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, _
FileName))
nFiles = nFiles + 1
List1.AddItem fso.BuildPath(fld.Path, FileName) ' Load ListBox
FileName = Dir() ' Get next file
DoEvents
Wend
Label1 = "Searching " & vbCrLf & fld.Path & "..."
nDirs = nDirs + 1
If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, _
nFiles)
Next
End If
End Function

Run the project, and click Command1. Enter the directory and file name to
search for. As each file is found, it is added to the list box. When the
process is complete, the number of files found is displayed in a message
box. The total size of the files is also shown.


REFERENCES
For information on other methods you can use to find a specific file, please
see the following article in the Microsoft Knowledge Base:

Q185476 HOWTO: Search Directories to Find or List Files
Additional query words: look subdirectory directory tree recursion recursive
File System Object filesystem
Keywords : kbVBp500 kbVBp600 kbGrpVB kbDSupport
Issue type : kbhowto
Technology :

Last Reviewed: April 24, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.


Send feedback to MSDN.Look here for MSDN Online resources.

--

Saludos,

Norman ;-)


"Eduardo" <ebar...@coto.com.ar> wrote in message
news:e04FcqY$AHA.1316@tkmsftngp04...

Iván Vega Rivera

unread,
Jun 25, 2001, 6:02:53 PM6/25/01
to
Sirve para recorrer todos los archivos de todas las carpetas de todas las
unidades. Debes ser más específico en cuanto a porque no funciona...

¿Agregaste la referencia a "Microsoft Scripting Runtime"?

"Eduardo" <ebar...@coto.com.ar> escribió en el mensaje

news:eDXlvva$AHA.2060@tkmsftngp04...

0 new messages