Afficher l'espace disque qu'occupe un dossier
Publié : ven. 28/mai/2004 21:55
Salut,
un petit code qui est en fait un outil intégré dans mon Bi-Exploreur et dont j'avais dis que je posterai le code.
il permet d'afficher la taille de chaque élément (fichier ou dossier) contenu d'un dossier.
bon, il n'y pas beaucoup de commentaires donc si un point vous intéresse et que vous ne le comprenez pas, demander moi 
un petit code qui est en fait un outil intégré dans mon Bi-Exploreur et dont j'avais dis que je posterai le code.
il permet d'afficher la taille de chaque élément (fichier ou dossier) contenu d'un dossier.
Code : Tout sélectionner
Enumeration
#Dossier1
#Dossier2
#Quitter
#Liste
#Affiche
#Selectionne
#SelectionInverse
#Texte
#Img
#ImgBouton11
#ImgBouton12
#ImgBouton21
#ImgBouton22
#ImgBouton31
#ImgBouton32
EndEnumeration
#TailleX = 380
#TailleY = 350
#ImgX = 160
Global Dossier.s
Dim Fichier.s(10000)
Dim Dossier.s(10000)
Dim Taille.l(10000)
Procedure ListIconGadgetXP(GadgetID.l, x.l, y.l, tx.l, ty.l, colonne.s, largeur.l, options.l)
; Même paramètres que pour une ListIconGadget, seule le paramètres options est obligatoire, mettre 0 si vous ne mettez pas d'option
ListIconGadget(GadgetID, x, y, tx, ty, colonne, largeur, options)
#LVM_SETEXTENDEDLISTVIEWSTYLE = 4150 : #LVS_EX_SUBITEMIMAGES = 2
hImageListS.l = SHGetFileInfo_("c:\", 0, @InfosFile.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_SYSICONINDEX | #SHGFI_SMALLICON)
SendMessage_(GadgetID(GadgetID), #LVM_SETIMAGELIST, #LVSIL_SMALL, hImageListS)
SendMessage_(GadgetID(GadgetID), #LVM_SETEXTENDEDLISTVIEWSTYLE, #LVS_EX_SUBITEMIMAGES, #LVS_EX_SUBITEMIMAGES)
EndProcedure
Procedure AddGadgetItemXP(GadgetID.l, Pos.l, Texte.s, IconPath.s)
; GadgetID : Numéro de la ListIconGadgetXP
; Pos : position à laqualle on souhaite insérer l'élément
; Texte : Texte de l'élément
; IconPath : Fichier dont on souhaite affiché l'icône
SHGetFileInfo_(IconPath, 0, @InfosFile.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON | #SHGFI_SMALLICON)
If Pos = -1
Pos = CountGadgetItems(GadgetID) + 1
EndIf
Structure LVITEM
mask.l
iItem.l
iSubitem.l
state.l
stateMask.l
pszText.l
cchTextMax.l
iImage.l
lParam.l
iIndent.l
iGroupId.l
cColumns.l
puColumns.l
EndStructure
var.LVITEM
var\mask = #LVIF_IMAGE | #LVIF_TEXT
var\iSubitem = 0
var\iItem = Pos
var\pszText = @Texte
var\iImage = InfosFile\iIcon
SendMessage_(GadgetID(GadgetID), #LVM_INSERTITEM, 0, @var)
EndProcedure
Procedure.s DisplaySize(Size.l)
Protected val.l, txt.s
approximation = 1 : unit = 0
If Size / 4 >= 1000 : unit = 1 : approximation = 2 : EndIf
If Size / 4 >= 10000 : unit = 1 : approximation = 1 : EndIf
If Size / 4 >= 1000000 : unit = 2 : approximation = 2 : EndIf
If Size / 4 >= 10000000 : unit = 2 : approximation = 1 : EndIf
Select unit
Case 1 : val = 1024 : txt = " Mo"
Case 2 : val = 1024 * 1024 : txt = " Go"
Default : val = 1 : txt = " Ko"
EndSelect
ProcedureReturn StrF(Size / (val * 4), approximation) + txt
EndProcedure
Procedure.l DirectorySize(Folder.s)
; Récupère la taille du dossier
Dim DirectoryList.s(10000)
Protected erreur.l, Directory.l, DirectorySize.l
erreur = 0
Directory = 1
DirectorySize = 0
If Right(Folder, 1) <> "\"
Folder = Folder + "\"
EndIf
Repeat
If ExamineDirectory(50, Folder, "*.*")
Repeat
type.l = NextDirectoryEntry()
name.s = DirectoryEntryName()
If type = 1
DirectorySize = DirectorySize + Int(FileSize(Folder + name) / 256 + 0.5)
EndIf
If type = 2 And name <> "." And name <> ".."
Directory = Directory + 1
DirectoryList(Directory) = Folder + name + "\"
EndIf
Until type = 0
Else
HideWindow(0, 1)
MessageRequester("Erreur", "Impossible d'analyser le dossier suivant :" + Chr(10) + Folder, 0)
HideWindow(0, 0)
EndIf
Folder = DirectoryList(Directory)
Directory = Directory - 1
Until Directory = 0
ProcedureReturn DirectorySize
EndProcedure
Procedure Pie3D(HDC.l, x.l, y.l, Rayon.l, Orientation.f, Hauteur.l, AngleDepart.f, AngleFin.f, Couleur.l)
; HDC : Handle du dessin
; X, Y : centre du camembert
; Rayon : Rayon du camembert
; Orientation : Orientation du camembert qui donne l'effet 3D. cette valeur doit être comprise entre 0 et 1
; Hauteur : Hauteur ou épaisseur du camembert
; AngleDepart : Angle de départ en radian
; AngleFin : Angle de fin en radian
; Couleur : Couleur du camenbert
Protected R.l, G.l, b.l, n.l, AX1.l, AY1.l, AX2.l, AY2.l, Rayon2.l
Rayon2 = Rayon * Orientation ; calul du rayon sur l'axe Y du camembert
y = y - Hauteur / 2 ; on recentre le camemenbert en fonction de l'épaisseur
R = Red(Couleur) : G = Green(Couleur) : b = Blue(Couleur) ; On décompose la couleur en RGB
AX2 = Int(x + 100 * Cos(AngleDepart)) ; calcul du point d'arriver
AY2 = Int(y + 100 * Sin(AngleDepart) * Orientation)
AX1 = Int(x + 100 * Cos(AngleFin)) ; calcul du point de départ
AY1 = Int(y + 100 * Sin(AngleFin) * Orientation)
For n = Hauteur To 1 Step - 1 ; On dessine l'épaisseur du camembert
FrontColor(R * (0.8 - 0.08 * n / Hauteur), G * (0.8 - 0.08 * n / Hauteur), b * (0.8 - 0.08 * n / Hauteur)) ; Choix de la couleur du bord du camembert
Pie_(HDC, x - Rayon, y - Rayon2 + n, x + Rayon, y + Rayon2 + n, AX1, AY1 + n, AX2, AY2 + n)
Next
FrontColor(R, G, b)
Pie_(HDC, x - Rayon, y - Rayon2, x + Rayon, y + Rayon2, AX1, AY1, AX2, AY2) ; on dessine le dessus du camembert
EndProcedure
Procedure Charge(Folder.s)
SetGadgetText(#Texte, "Opération en cours ...")
UseImage(#Img)
StartDrawing(ImageOutput())
Box(0, 20, #ImgX, 300, GetSysColor_(#COLOR_3DFACE))
StopDrawing()
SetGadgetState(#Img, UseImage(#Img))
Temps = GetTickCount_()
UpdateWindow_(WindowID())
If Right(Folder, 1) <> "\"
Folder = Folder + "\"
EndIf
If ExamineDirectory(0, Folder, "*.*")
PosF = 0
PosD = 0
Repeat
FileType = NextDirectoryEntry()
Nom.s = DirectoryEntryName()
If FileType = 1
Fichier(PosF) = Nom
PosF + 1
ElseIf FileType = 2 And Nom <> "." And Nom <> ".."
Dossier(PosD) = Nom
PosD + 1
EndIf
Until FileType = 0
SortArray(Fichier(), 2, 0, PosF - 1)
SortArray(Dossier(), 2, 0, PosD - 1)
For n = 0 To PosD - 1
AddGadgetItemXP(#Liste, n, Dossier(n), Folder + Dossier(n))
Taille(n) = DirectorySize(Folder + Dossier(n))
SetGadgetItemText(#Liste, n, DisplaySize(Taille(n)), 1)
UpdateWindow_(WindowID())
Repeat ; pour quitter en cours d'analyse
Event = WindowEvent()
If Event = #WM_CLOSE
End
EndIf
Until Event = 0
Next
For n = 0 To PosF - 1
AddGadgetItemXP(#Liste, PosD + n, Fichier(n), Folder + Fichier(n))
Taille(PosD + n) = Int(FileSize(Folder + Fichier(n)) / 256 + 0.5)
SetGadgetItemText(#Liste, PosD + n, DisplaySize(Taille(PosD + n)), 1)
UpdateWindow_(WindowID())
Repeat ; pour quitter en cours d'analyse
Event = WindowEvent()
If Event = #WM_CLOSE
End
EndIf
Until Event = 0
Next
TotalDossier = 0
For n = 0 To PosD - 1
TotalDossier = TotalDossier + Taille(n)
Next
TotalFichier = 0
For n = PosD To PosD + PosF - 1
TotalFichier = TotalFichier + Taille(n)
Next
Total = TotalFichier + TotalDossier
Dossier = Folder
If Total
For n = 0 To PosD + PosF - 1
SetGadgetItemText(#Liste, n, StrF(Taille(n) / Total * 100, 2), 2)
Next
#DecalageX = 5
#DecalageY = 140
#Cx = 80
#Cy = 85
UseImage(#Img)
HDC = StartDrawing(ImageOutput())
Box(0, 0, #ImgX, 300, GetSysColor_(#COLOR_3DFACE))
DrawingMode(1)
FontID = LoadFont(0, "Tahoma", 8, #PB_Font_HighQuality | #PB_Font_Bold)
DrawingFont(FontID)
FrontColor(0, 0, 0)
Locate(5, 5)
DrawText("Répartition :")
CloseFont(0)
FontID = LoadFont(0, "Tahoma", 8, #PB_Font_HighQuality)
DrawingFont(FontID)
Box(#DecalageX, #DecalageY, 14, 9, RGB(94, 164, 235))
FrontColor(0, 0, 0)
Locate(#DecalageX + 20, #DecalageY - 2)
If PosF > 1
DrawText(Str(PosF) + " Fichiers")
Else
DrawText(Str(PosF) + " Fichier")
EndIf
Locate(#DecalageX + 20, #DecalageY + 11)
DrawText(DisplaySize(TotalFichier) + " ( " + StrF(TotalFichier / Total * 100, 1) + " % )")
Box(#DecalageX, #DecalageY + 35, 14, 9, RGB(177, 177, 255))
FrontColor(0, 0, 0)
Locate(#DecalageX + 20, #DecalageY + 33)
If PosD > 1
DrawText(Str(PosD) + " Dossiers")
Else
DrawText(Str(PosD) + " Dossier")
EndIf
Locate(#DecalageX + 20, #DecalageY + 33 + 13)
DrawText(DisplaySize(TotalDossier) + " ( " + StrF(TotalDossier / Total * 100, 1) + " % )")
Locate(#DecalageX + 20, #DecalageY + 70 - 2)
DrawText("Total : " + DisplaySize(Total))
AngleF.f = TotalFichier / Total * 2 * #Pi
If AngleF < 0.05
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, 0, 2 * #Pi, RGB(177, 177, 255))
ElseIf AngleF <= #Pi / 2
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi + AngleF, 0, RGB(177, 177, 255))
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi, #Pi + AngleF, RGB(94, 164, 235))
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, 0, #Pi, RGB(177, 177, 255))
ElseIf AngleF <= 3 * #Pi / 2
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi, #Pi + AngleF, RGB(94, 164, 235))
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi + AngleF, #Pi, RGB(177, 177, 255))
Else
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi, 0, RGB(94, 164, 235))
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, #Pi + AngleF, #Pi, RGB(177, 177, 255))
Pie3D(HDC, #Cx, #Cy, 70, 0.55, 7, 0, #Pi + AngleF, RGB(94, 164, 235))
EndIf
CloseFont(0)
StopDrawing()
SetGadgetState(#Img, UseImage(#Img))
EndIf
Else
HideWindow(0, 1)
MessageRequester("Erreur", "Impossible d'analyser le dossier suivant :" + Chr(10) + Folder, 0)
HideWindow(0, 0)
EndIf
SetGadgetText(#Texte, "Opération terminée en " + StrF((GetTickCount_() - Temps) / 1000, 1) + "s")
EndProcedure
Procedure MoveFileToRecycleBin(DeletedFile.s)
Protected lpFileOp.SHFILEOPSTRUCT
If FileSize(DeletedFile) <> - 1
If Right(DeletedFile, 1) = "\"
DeletedFile = Left(DeletedFile, Len(DeletedFile) - 1)
EndIf
Mem = AllocateMemory(Len(DeletedFile) + 2)
If Mem
lpFileOp\hwnd = 0
lpFileOp\pTo = 0
lpFileOp\wFunc = #FO_DELETE
lpFileOp\pFrom = Mem
lpFileOp\fFlags = #FOF_ALLOWUNDO | #FOF_NOCONFIRMATION
CopyMemoryString(DeletedFile, @Mem)
CopyMemoryString(Chr(0))
CopyMemoryString(Chr(0))
SHFileOperation_(@lpFileOp)
FreeMemory(0)
EndIf
EndIf
EndProcedure
Procedure.s GetSpecialFolderLocation(lngCSIDL.l)
Protected lngRet.l, strLocation.s, pidl.l
strLocation = Space(260)
lngRet = SHGetSpecialFolderLocation_(0, lngCSIDL, @pidl)
If lngRet = 0
SHGetPathFromIDList_(pidl, @strLocation)
If lngRet = 0
strLocation = RTrim(strLocation)
If Right(strLocation, 1) <> "\"
strLocation = strLocation + "\"
EndIf
ProcedureReturn strLocation
EndIf
CoTaskMemFree_(pidl)
EndIf
EndProcedure
;- Debut du programme
; Création de la fenêtre et dela GadgetList
If OpenWindow(0, 0, 0, #ImgX + #TailleX - 1, #TailleY - 1, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible, " Espace disque") = 0 Or CreateGadgetList(WindowID()) = 0
End
EndIf
SetWindowLong_(WindowID(), #GWL_EXSTYLE, GetWindowLong_(WindowID(), #GWL_EXSTYLE) | #WS_EX_TOOLWINDOW) ; choix de la barre d'outil réduite
ResizeWindow(#ImgX + #TailleX, #TailleY) ; redimensionne la fenetre
ResizeWindow(#ImgX + #TailleX, 2 * #TailleY - WindowHeight())
ShowWindow_(WindowID(), #SW_SHOW) ; montre la fenêtre
SetWindowPos_(WindowID(), -1, 0, 0, 0, 0, #SWP_NOSIZE | #SWP_NOMOVE) ; mets la fenêtre toujours au premier plan
Dossier = "C:\"
ExplorerComboGadget(#Affiche, #ImgX + 1, 1, #TailleX, 200, Dossier)
; Hauteur du comboboxgadget
GetWindowRect_(GadgetID(#Affiche), Combo.RECT)
Hauteur = Combo\bottom - Combo\top
ListIconGadgetXP(#Liste, #ImgX + 1, Hauteur, #TailleX, 350 - 1 - Hauteur, "Fichier", #TailleX - 180, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection | #PB_ListIcon_MultiSelect)
AddGadgetColumn(#Liste, 1, "Taille", 80)
AddGadgetColumn(#Liste, 2, "%", 50)
TextGadget(#Texte, 1, 350 - 15, #ImgX - 4, 15, "", #PB_Text_Right)
CreateImage(#Img, #ImgX, 300)
StartDrawing(ImageOutput())
Box(0, 0, #ImgX, 300, GetSysColor_(#COLOR_3DFACE))
StopDrawing()
ImageGadget(#Img, 0, 0, #ImgX, 200, UseImage(#Img))
Charge(Dossier)
Repeat
Event = WaitWindowEvent()
If Event = #PB_EventGadget
Select EventGadgetID() ; boutons, zone de texte, ...
Case #Affiche
ClearGadgetItemList(#Liste)
Charge(GetGadgetText(#Affiche))
Case #Liste
Pos = GetGadgetState(#Liste)
If Pos <> - 1
Select EventType()
Case #PB_EventType_LeftDoubleClick
Fichier.s = GetGadgetItemText(#Liste, Pos, 0)
If FileSize(Dossier + Fichier) >= 0 ; on teste si c'est un fichier ou un dossier
ElseIf FileSize(Dossier + Fichier) = -2
ClearGadgetItemList(#Liste)
Charge(Dossier + Fichier)
SetGadgetText(#Affiche, Dossier)
EndIf
EndSelect
EndIf
EndSelect
EndIf
Until Event = #PB_EventCloseWindow
End
