tincho   13-03-2024, 23:53
#1
Hola amigos.
Le comparto una función que sirve para averiguar cuando ocupa un directorio, o mejor dicho cuanto es la suma de todos los archivos que contiene.
En realidad son dos funciones una que lista todos los archivos (ScanFiles) y otra que los contabiliza (DirSize).

El primer argumento es obligatorio pues es el directorio a analizar y el segundo, que es booleano indica si se contabilizaran los enlaces simbólicos
Código:
'' <b>RAD Extension.</b><br>
'' Return the directory size in bytes. The first argument is mandatory because it is the directory to be analyzed and the second, which is boolean, indicates whether symbolic links will be counted.
Public Function DirSize(sDir As String, bSymbolic As Boolean) As Long
  Dim a As String[]
  Dim f As String
  Dim z As Long
  a = ScanFiles(sDir,,,, True) 'Esta es la otra función
  For Each f In a
    z = z + Stat2(f)["size"]
  Next
  Return z
End

Código:
'' <b>RAD Extension.</b><br>
'' Return a files list nested in a directory using a find command
Public Function ScanFiles(sPath As String, Optional ext As String[], Optional maxdepth As Integer, Optional sFilter As String, Optional bSymbolic As Boolean) As String[]

  Dim out As New String[]
  Dim t As String
  Dim ex As String
  Dim sTmp As String
  Dim aOut As New String[]
  Dim sType As String
  Dim sMax As String

  If maxdepth > 0 Then
    sMax = " -maxdepth " & CStr(maxdepth)
  Endif

  If bSymbolic Then
    sType = " -type f,l"
  Else
    sType = " -type f"
  Endif

  If Exist(sPath) Then
    If ext Then
      Select ext.Count
          'Case 0
          '  Shell "find '" & sPath & "'" & sMax & sType & " -printf '%p\n' 2>&1" To t
        Case 1
          Shell "find '" & sPath & "'" & sMax & sType & " -name '*.'" & ext[0] & " -printf '%p\n' 2>&1" To t
        Case Else
          ex = "\\( -name \"*."
          ex &= ext.Join("\" -o -name \"*.")
          ex &= "\" \\)"
          Shell "find '" & sPath & "'" & sMax & sType & ex & " -printf '%p\n' 2>&1" To t
      End Select
    Else
      Shell "find '" & sPath & "'" & sMax & sType & " -printf '%p\n' 2>&1" To t
    Endif

    out = Split(t, "\n")

    If out.Count > 0 Then
      If out[out.Max] = "" Then
        out.Remove(out.Max, 1)
      Endif
    Endif

  Endif

  If sFilter <> "" Then
    For Each sTmp In out
      If InStr(sTmp, sFilter) > 0 Then
        aOut.Add(sTmp)
      Endif
    Next
  Else
    aOut = out.Copy()
  Endif

  Return aOut

End

1 Saludo.
  
Usuarios navegando en este tema: 1 invitado(s)
Powered By MyBB, © 2002-2024 MyBB Group.
Made with by Curves UI.