Skip to content

Commit 4a275db

Browse files
committed
Added Get-ItemSize
1 parent bdc911a commit 4a275db

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

Tests/Get-ItemSize.Tests.ps1

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Import-Module .\WindowsDiskCleanup\WindowsDiskCleanup.psm1 -Force
2+
3+
Describe 'Get-ItemSize.Tests' {
4+
BeforeAll {
5+
$testFolder = "$PSScriptRoot\TestFolder"
6+
$nestedFolder = "$testFolder\Nested"
7+
$testFile1 = "$testFolder\File1.txt"
8+
$testFile2 = "$nestedFolder\File2.txt"
9+
}
10+
11+
BeforeEach {
12+
if (-not (Test-Path $testFolder)) {
13+
New-Item -Path $testFolder -ItemType Directory -Force | Out-Null
14+
}
15+
if (-not (Test-Path $nestedFolder)) {
16+
New-Item -Path $nestedFolder -ItemType Directory -Force | Out-Null
17+
}
18+
Set-Content -Path $testFile1 -Value ('A' * 1024) -NoNewline # 1 KB
19+
Set-Content -Path $testFile2 -Value ('B' * 2048) -NoNewline # 2 KB
20+
}
21+
22+
AfterEach {
23+
if (Test-Path $testFolder) {
24+
Remove-Item -Path $testFolder -Recurse -Force
25+
}
26+
}
27+
28+
It 'should calculate the correct size of a folder with files' {
29+
$result = Get-ItemSize -Path $testFolder
30+
$result | Should -Be 3072 # 1 KB + 2 KB
31+
}
32+
33+
It 'should return 0 for an empty folder' {
34+
Remove-Item -Path $testFile1, $testFile2 -Force
35+
$result = Get-ItemSize -Path $testFolder
36+
$result | Should -Be 0
37+
}
38+
39+
It 'should calculate the correct size for nested folders' {
40+
$result = Get-ItemSize -Path $testFolder
41+
$result | Should -Be 3072 # 1 KB + 2 KB
42+
}
43+
44+
It "shouldn't throw an error for an invalid path" {
45+
$result = Get-ItemSize -Path "$PSScriptRoot\InvalidPath"
46+
$result | Should -Be 0
47+
}
48+
49+
It 'should calculate the correct size of a single file' {
50+
$fileInfo = Get-Item -Path $testFile1
51+
$result = Get-ItemSize -File $fileInfo
52+
$result | Should -Be 1024 # 1 KB
53+
}
54+
55+
It 'should return 0 for a non-existent file' {
56+
$fileInfo = [System.IO.FileInfo]::new("$PSScriptRoot\NonExistentFile.txt")
57+
$result = Get-ItemSize -File $fileInfo
58+
$result | Should -Be 0
59+
}
60+
}

WindowsDiskCleanup/Public/Get-DiskSpace.ps1

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ function Get-DiskSpace {
2424
"KB" { $space / 1KB }
2525
}
2626
}
27-
}
28-
29-
Get-DiskSpace -SpaceType Free -Unit GB
27+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Get-ItemSize {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "File")]
5+
[System.IO.FileSystemInfo]$File,
6+
7+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Path")]
8+
[string]$Path,
9+
10+
[ValidateSet("GB", "MB", "KB", "Bytes")]
11+
[string]$Unit = "Bytes"
12+
)
13+
14+
process {
15+
$size = 0
16+
17+
if ($PSCmdlet.ParameterSetName -eq 'Path') {
18+
if (-Not (Test-Path -Path $Path)) {
19+
return 0
20+
}
21+
22+
$size = (Get-ChildItem -Recurse -File $Path | Measure-Object -Property Length -Sum).Sum
23+
if ($null -eq $size) {
24+
return 0
25+
}
26+
}
27+
elseif ($PSCmdlet.ParameterSetName -eq 'File') {
28+
if (-Not (Test-Path -Path $File.FullName)) {
29+
return 0
30+
}
31+
32+
$size = $File.Length
33+
}
34+
35+
switch ($Unit) {
36+
"GB" { return [math]::Round($size / 1GB, 2) }
37+
"MB" { return [math]::Round($size / 1MB, 2) }
38+
"KB" { return [math]::Round($size / 1KB, 2) }
39+
"Bytes" { return $size }
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)