Get total sub-folder sizes and number of files into csv


i need total size , amount of files of many sub-folders.

the script below gives me amount of files per sub-folder, doesn't show subfolder itself.

i total size besides number of files.

then error if sub-folder path doesn't exist.

how fix ?

$search1 = "x:\root-folders\*"  $search2 = "y:\root-folders\*"    $folders = get-childitem -path $search1, $search2    $subfolder = "\child-folder\doc"    foreach ($folder in $folders)  {  (get-childitem $folder\$subfolder).count  }  

this think you're searching for, modified code bit, read comments. requires powershell v3 though, let me know if need v2.

# folders search in $search = @('x:\root-folders', 'y:\root-folders')  # search folder , recurse directory depth of 2, change accordingly $folders = get-childitem -path $search -recurse -directory -depth 2  # subfolder filter in folder above, used @ last line below $subfolder = '\child-folder\doc'  # defining array store pscustomobjects in foreach statement $array = @()  foreach ($folder in $folders) {     $foldername = $folder.fullname      # search files in subdirectory     $files = get-childitem $foldername -attributes !directory      # calculating size in mb files found     $size = $null     $files | foreach-object -process {         $size += $_.length     }       $sizeinmb = [math]::round(($size / 1mb), 1)       # adding pscustomobjets entries array     $array += [pscustomobject]@{         folder   = $foldername         count    = $files.count         'size(mb)' = $sizeinmb     } }  # return subfolders $array  # return subfolders matching variable $subfolder $array | folder -match $subfolder


http://www.ruudborst.nl





Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

2008 Windows Deployment Server Properties Error

Can no longer user MS Update - Files required to use Microsoft Update are no longer registered

How do a find data in one file, search for it in another file and if not found, write a custom message to another file