Buscar en este blog

Loading...

Wednesday, August 08, 2007

Código VB6 para obtener lista de ficheros de una carpeta

Private Function AllFiles(ByVal FullPath As String) _
As String()
'***************************************************
'PURPOSE: Returns all files in a folder using
'the FileSystemObject

'PARAMETER: FullPath = FullPath to folder for
'which you want all files

'RETURN VALUE: An array containing a list of
'all file names in FullPath, or a 1-element
'array with an empty string if FullPath
'does not exist or it has no files

'REQUIRES: Reference to Micrsoft Scripting
' Runtime

'EXAMPLE:

'Dim sFiles() as string
'dim lCtr as long
'sFiles = AllFiles("C:\Windows\System")
'For lCtr = 0 to Ubound(sFiles)
' Debug.Print sfiles(lctr)
'Next

'REMARKS: The FileSystemObject does not
'Allow for the use of wild cards (e.g.,
'*.txt.) If this is what you need, see
'http://wwww.freevbcode.com/ShowCode.asp?ID=1331
'************************************************

Dim oFs As New FileSystemObject
Dim sAns() As String
Dim oFolder As Folder
Dim oFile As File
Dim lElement As Long

ReDim sAns(0) As String
If oFs.FolderExists(FullPath) Then
Set oFolder = oFs.GetFolder(FullPath)

For Each oFile In oFolder.Files
lElement = IIf(sAns(0) = "", 0, lElement + 1)
ReDim Preserve sAns(lElement) As String
sAns(lElement) = oFile.Name
Next
End If

AllFiles = sAns
ErrHandler:
Set oFs = Nothing
Set oFolder = Nothing
Set oFile = Nothing
End Function

No comments:

Post a Comment