Taille d'un dossier et affichage d'une taille

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Le Soldat Inconnu
Messages : 4312
Inscription : mer. 28/janv./2004 20:58
Localisation : Clermont ferrand OU Olsztyn
Contact :

Taille d'un dossier et affichage d'une taille

Message par Le Soldat Inconnu »

Code : Tout sélectionner

Procedure.l DirectorySize(dossier.s, filtre.s)
  Dim DirectoryList.s(100)
  Protected erreur.l
  Protected Directory.l
  Protected DirectorySize.l
  erreur = 0
  Directory = 1
  DirectorySize = 0
  
  If Right(dossier, 1) <> "\"
    dossier = dossier + "\"
  EndIf
  
  Repeat
    If ExamineDirectory(50, dossier, filtre)
      Repeat
        type.l = NextDirectoryEntry()
        name.s = DirectoryEntryName()
        If type = 1
          NomFichier$ = dossier + name
          DirectorySize = DirectorySize + FileSize(NomFichier$)
        EndIf
        If type = 2 And name <> "." And name <> ".."
          Directory = Directory + 1
          DirectoryList(Directory) = dossier + name + "\"
        EndIf
      Until type = 0
    Else
      erreur = 1
    EndIf
    
    For n = 1 To Directory
      DirectoryList(n) = DirectoryList(n + 1)
    Next
    Directory = Directory - 1
    SortArray(DirectoryList(), 2, 1, Directory)
    dossier = DirectoryList(1)
    
  Until Directory = 0
  
  If erreur = 1
    ProcedureReturn erreur
  Else
    ProcedureReturn DirectorySize
  EndIf
EndProcedure


Procedure.s DisplaySize(size.l, unit.l, approximation.l, auto.l)
  ; size = taille du dossier
  ; unit = unité de la taille, 0 = octets, 1 = Ko, 2 = Mo
  ; approximation = nombre de chiffre après la virgule
  ; si auto = 1, choix de l'unité automatique et approximation automatique
  Protected val.l
  Protected txt.s
  If auto
    approximation = 0 : unit = 0
    If size >= 1000 : unit = 1 : approximation = 2 : EndIf
    If size >= 10000 : unit = 1 : approximation = 1 : EndIf
    If size >= 1000000 : unit = 2 : approximation = 2 : EndIf
    If size >= 10000000 : unit = 2 : approximation = 1 : EndIf
  EndIf
  Select unit
    Case 1 : val = 1024 : txt = " Ko"
    Case 2 : val = 1024 * 1024 : txt = " Mo"
    Default : val = 1 : txt = " octects"
  EndSelect
  ProcedureReturn StrF(size / val, approximation) + txt
EndProcedure




taille = directorysize("c:\program files\purebasic\", "*.*")
Debug taille
Debug displaysize(taille, 2, 2, 1)
Je ne suis pas à moitié Polonais mais ma moitié est polonaise ... Vous avez suivi ?

[Intel quad core Q9400 2.66mhz, ATI 4870, 4Go Ram, XP (x86) / 7 (x64)]