Home > Enterprise >  PowerShell script - Loop list of folders to get file count and sum of files for each folder listed
PowerShell script - Loop list of folders to get file count and sum of files for each folder listed

Time:09-15

I want to get the file count & the sum of files for each individual folder listed in DGFoldersTEST.txt. However, I’m currently getting the sum of all 3 folders. And now I'm getting 'Index was outside the bounds of the array' error message.

$DGfolderlist = Get-Content -Path C:\DiskGroupsFolders\DGFoldersTEST.txt

$FolderSize =@() 
$int=0

Foreach ($DGfolder in $DGfolderlist)
{
$FolderSize[$int] = 
    Get-ChildItem -Path $DGfolderlist -File -Recurse -Force -ErrorAction SilentlyContinue | 
    Measure-Object -Property Length -Sum | 
    Select-Object -Property Count, @{Name='Size(MB)'; Expression={('{0:N2}' -f($_.Sum/1mb))}}

Write-Host $DGfolder
Write-Host $FolderSize[$int]
$int  
}

CodePudding user response:

To explain the error, you're trying to assign a value at index $int of your $FolderSize array, however, when arrays are initialized using the array subexpression operator @(..), they're intialized with 0 Length, hence why the error. It's different as to when you would initialize them with a specific Length:

$arr = @()
$arr.Length       # 0
$arr[0] = 'hello' # Error

$arr = [array]::CreateInstance([object], 10)
$arr.Length       # 10
$arr[0] = 'hello' # all good

As for how to approach your code, since you don't really know how many items will come as output from your loop, initializing an array with a specific Length is not possible. PowerShell offers the = operator for adding elements to it, however this is a very expensive operation and not a very good idea because each time we append a new element to the array, a new array has to be created, this is because arrays are of a fixed size. See this answer for more information and better approaches.

You can simply let PowerShell capture the output of your loop by assigning the variable to the loop itself:

$FolderSize = foreach ($DGfolder in $DGfolderlist) {
    Get-ChildItem -Path $DGfolder -File -Recurse -Force -ErrorAction SilentlyContinue |
        Measure-Object -Property Length -Sum | 
        Select-Object @(
            @{ Name = 'Folder'; Expression = { $DGfolder }}
            'Count'
            @{ Name = 'Size(MB)'; Expression = { ($_.Sum / 1mb).ToString('N2') }}
        )
}
  • Related