Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
powershell_usedspace [2020/07/17 12:18] – créée nekanpowershell_usedspace [2021/03/05 16:10] (Version actuelle) nekan
Ligne 1: Ligne 1:
-~~CLOSETOC~~ 
 ====== Connaître l'espace utilisé par un dossier ====== ====== Connaître l'espace utilisé par un dossier ======
 +<label type="info">Création</label> --- //[[nekan@shyrkasystem.com|Nicolas THOREZ]] 2020/07/17 12:07//
  
 une petite fonction utile pour connaître l'espace utilisé par un dossier, seul ou avec ces sous-dossiers. une petite fonction utile pour connaître l'espace utilisé par un dossier, seul ou avec ces sous-dossiers.
  
-<sxh powershell><#+<sxh powershell>function Get-FolderUsedSpace() 
 +
 +<#
  .SYNOPSIS  .SYNOPSIS
  Affiche la taille d'un dossier.  Affiche la taille d'un dossier.
Ligne 19: Ligne 21:
   
  .PARAMETER HumanReadable  .PARAMETER HumanReadable
- Renvoie l'espace utilisé de manière lisible, avec l'unit" (oKoMoGoTo).+ Renvoie l'espace utilisé de manière lisible, avec l'unité (BKBMBGBTB).
   
  .EXAMPLE  .EXAMPLE
- Display-FolderUsedSpace -Path "c:\Windows" -Recurse+ Get-FolderUsedSpace -Path "c:\Windows" -Recurse
   
  .OUTPUTS  .OUTPUTS
- System.String, System.int32+ PSCustomObject
   
  .NOTES  .NOTES
Ligne 36: Ligne 38:
  Plus d'informations sur https://www.shyrkasystem.com  Plus d'informations sur https://www.shyrkasystem.com
 #> #>
- +  
-# Paramètres + # Paramètres 
-param + param 
-+
- [string]$Path = (Get-Location).Path, + [string]$Path = (Get-Location).Path, 
- [switch]$Recurse = $false, + [switch]$Recurse = $false, 
- [switch]$HumanReadable = $false + [switch]$HumanReadable = $false 
-+
- +  
-# Initialisation des variables + # Initialisation des variables 
-$FileList = @() + $FileList = @() 
-$Size = 0 + $Size = 0 
- + $Unit = "B" 
-# Création de la liste des fichiers +  
-$FileList = Get-ChildItem -Path $Path -include * -Recurse:$Recurse -force + # Création de la liste des fichiers 
- + $FileList = Get-ChildItem -Path $Path -include * -Recurse:$Recurse -force 
-# Calcul +  
-ForEach ($File in $FileList) + # Calcul 
-+ ForEach ($File in $FileList) 
- # Ajout de la taille du fichier dans le total +
- $Size += $File.length + # Ajout de la taille du fichier dans le total 
-+ $Size += $File.length 
- +
-# Affichage du résultat +
-if ($HumanReadable) +
-{+
  # Calcul de la fraction  # Calcul de la fraction
  $Factor = [int][math]::Floor("$Size".Length/3)  $Factor = [int][math]::Floor("$Size".Length/3)
   
-Sélection de l'unité selon la fraction +Traitement de la lisibilité 
- switch ($Factor)+ if ($HumanReadable)
  {  {
- + # Sélection de l'unité selon la fraction 
- $Unit = "B"+ switch ($Factor) 
 +
 + 0 { 
 + $Unit = "B" 
 +
 + 1 { 
 + $Unit = "KB" 
 +
 + 2 { 
 + $Unit = "MB" 
 +
 + 3 { 
 + $Unit = "GB" 
 +
 + default { 
 + $Unit = "TB" 
 + }
  }  }
- 1 { +  
- $Unit = "KB" + # Calcule de la taille lisible 
- } + if ($Unit -ne "B") 
- 2 { +
- $Unit "MB+ $Divider = "1$Unit
- + $Size [math]::Round($Size / $Divider, 2)
-+
- $Unit = "GB" +
-+
- default { +
- $Unit "TB"+
  }  }
 + }
 +
 + # Création des propriétés de l'objet
 + $Hash = [ordered]@{
 + Path = $Path
 + Size = $Size
 + Unit = $Unit
  }  }
   
-Affiche du résultat +Affichage de l'objet 
- if ($Unit -eq "B"+ New-Object PSObject -Property $Hash
-+
- Write-Host "$Size B" +
-+
- else +
-+
- $Divide = "1$Unit" +
- $HumanReadableSize = [math]::Round($Size / $Divide, 2) +
- Write-Host "$HumanReadableSize $Unit" +
-+
-+
-else +
-+
- Write-Host $Size+
 }</sxh> }</sxh>
- 
- --- //[[nekan@shyrkasystem.com|Nicolas THOREZ]] 2020/07/17 12:07// 
  
 ~~DISCUSSION~~ ~~DISCUSSION~~
  • powershell_usedspace.1594981087.txt.gz
  • Dernière modification : 2020/07/17 12:18
  • de nekan