Compartir a través de


Cómo: Consultar los archivos o archivos más grandes en un árbol de directorios (LINQ) (Visual Basic)

En este ejemplo se muestran cinco consultas relacionadas con el tamaño de archivo en bytes:

  • Cómo recuperar el tamaño en bytes del archivo más grande.

  • Cómo recuperar el tamaño en bytes del archivo más pequeño.

  • Cómo recuperar el archivo más grande o más pequeño de una o varias carpetas dentro de una carpeta raíz especificada.

  • Cómo recuperar una secuencia como los 10 archivos más grandes.

  • Cómo ordenar archivos en grupos en función de su tamaño de archivo en bytes, ignorando los archivos que son menores que un tamaño especificado.

Ejemplo

En el ejemplo siguiente se incluyen cinco consultas independientes que muestran cómo consultar y agrupar archivos, según su tamaño de archivo en bytes. Puede modificar fácilmente estos ejemplos para basar la consulta en alguna otra propiedad del FileInfo objeto.

Module QueryBySize  
    Sub Main()  
  
        ' Change the drive\path if necessary  
        Dim root As String = "C:\Program Files\Microsoft Visual Studio 9.0"  
  
        'Take a snapshot of the folder contents  
        Dim dir As New System.IO.DirectoryInfo(root)  
        Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)  
  
        ' Return the size of the largest file  
        Dim maxSize = Aggregate aFile In fileList Into Max(GetFileLength(aFile))  
  
        'Dim maxSize = fileLengths.Max  
        Console.WriteLine("The length of the largest file under {0} is {1}", _  
                          root, maxSize)  
  
        ' Return the FileInfo object of the largest file  
        ' by sorting and selecting from the beginning of the list  
        Dim filesByLengDesc = From file In fileList _  
                              Let filelength = GetFileLength(file) _  
                              Where filelength > 0 _  
                              Order By filelength Descending _  
                              Select file  
  
        Dim longestFile = filesByLengDesc.First  
  
        Console.WriteLine("The largest file under {0} is {1} with a length of {2} bytes", _  
                          root, longestFile.FullName, longestFile.Length)  
  
        Dim smallestFile = filesByLengDesc.Last  
  
        Console.WriteLine("The smallest file under {0} is {1} with a length of {2} bytes", _  
                                root, smallestFile.FullName, smallestFile.Length)  
  
        ' Return the FileInfos for the 10 largest files  
        ' Based on a previous query, but nothing is executed  
        ' until the For Each statement below.  
        Dim tenLargest = From file In filesByLengDesc Take 10  
  
        Console.WriteLine("The 10 largest files under {0} are:", root)  
  
        For Each fi As System.IO.FileInfo In tenLargest  
            Console.WriteLine("{0}: {1} bytes", fi.FullName, fi.Length)  
        Next  
  
        ' Group files according to their size,  
        ' leaving out the ones under 200K  
        Dim sizeGroups = From file As System.IO.FileInfo In fileList _  
                         Where file.Length > 0 _  
                         Let groupLength = file.Length / 100000 _  
                         Group file By groupLength Into fileGroup = Group _  
                         Where groupLength >= 2 _  
                         Order By groupLength Descending  
  
        For Each group In sizeGroups  
            Console.WriteLine(group.groupLength + "00000")  
  
            For Each item As System.IO.FileInfo In group.fileGroup  
                Console.WriteLine("   {0}: {1}", item.Name, item.Length)  
            Next  
        Next  
  
        ' Keep the console window open in debug mode  
        Console.WriteLine("Press any key to exit.")  
        Console.ReadKey()  
  
    End Sub  
  
    ' This method is used to catch the possible exception  
    ' that can be raised when accessing the FileInfo.Length property.  
    ' In this particular case, it is safe to ignore the exception.  
    Function GetFileLength(ByVal fi As System.IO.FileInfo) As Long  
        Dim retval As Long  
        Try  
            retval = fi.Length  
        Catch ex As FileNotFoundException  
            ' If a file is no longer present,  
            ' just return zero bytes.
            retval = 0  
        End Try  
  
        Return retval  
    End Function  
End Module  

Para devolver uno o varios objetos completos FileInfo , la consulta primero debe examinar cada uno del origen de datos y, a continuación, ordenarlos por el valor de su propiedad Length. A continuación, puede devolver el único o la secuencia con la mayor longitud. Use First para devolver el primer elemento de una lista. Use Take para devolver el primer número n de elementos. Especifique un criterio de ordenación descendente para colocar los elementos más pequeños al principio de la lista.

La consulta llama a un método independiente para obtener el tamaño del archivo en bytes y así manejar la posible excepción que se generaría si un archivo se hubiera eliminado en otro subproceso durante el período de tiempo desde que el objeto FileInfo fue creado en la llamada a GetFiles. Aunque el FileInfo objeto ya se ha creado, se puede producir la excepción porque, la primera vez que se accede a la propiedad, un FileInfo objeto intentará actualizar su Length propiedad usando el tamaño más reciente en bytes. Al incluir esta operación en un bloque try-catch fuera de la consulta, seguimos la regla de evitar las operaciones en las consultas que pueden producir efectos secundarios. En general, debe tener mucho cuidado al consumir excepciones para asegurarse de que no deja una aplicación en un estado desconocido.

Compilación del código

Cree un proyecto de aplicación de consola de Visual Basic con una instrucción Imports para el espacio de nombres System.Linq.

Consulte también