Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Directory Size and File count

Posted on 2013-05-21
5
Medium Priority
?
418 Views
Last Modified: 2013-05-21
I have the following script to locate a file called "user.db". How can I get a file count and directory size when the file is found.

$Computer = $env:computername
$strName = $env:username
$Filname = $Computer + "_" + $strName + ".txt"
Get-ChildItem -Path c:\ -Filter "user.db" -Recurse -ErrorAction SilentlyContinue
foreach-object { $Computer + "," + $Computer +"," +  $_.Fullname + "," + $_.Length + "," +$_.LastAccessTime} |Out-File c:\Results\$Filname –width 1024
0
Comment
Question by:grayjfiii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:BT15
ID: 39184913
I added the directory name, count of files in the directory, and directory size

$Computer = $env:computername
$strName = $env:username
$Filname = $Computer + "_" + $strName + ".txt"
Get-ChildItem -Path c:\ -Filter "user.db" -Recurse -ErrorAction SilentlyContinue | foreach-object { 
$dirsize = 0
$files = Get-ChildItem $_.directory -Recurse
foreach ($file in $files) {
	$dirsize += $file.length
}
	 
$Computer + "," + $Computer +"," +  $_.Fullname + "," + $_.Length + "," + $_.directory + "," + $files.count + "," + $dirsize + "," +$_.LastAccessTime }  |Out-File h:\$Filname –width 1024 

Open in new window

0
 

Author Comment

by:grayjfiii
ID: 39185080
How can I add this to it to get MB for file size

$colItems = (Get-ChildItem C:\Scripts | Measure-Object -property length -sum)
"{0:N2}" -f ($colItems.sum / 1MB) + " MB"
0
 
LVL 7

Accepted Solution

by:
BT15 earned 2000 total points
ID: 39185295
i am trying to piece together what exactly you are asking. The snippet you have provided already does the conversion.

are you wanting the filesize i provided above to be converted to MB?

$Computer = $env:computername
$strName = $env:username
$Filname = $Computer + "_" + $strName + ".txt"
Get-ChildItem -Path c:\ -Filter "user.db" -Recurse -ErrorAction SilentlyContinue | foreach-object { 
$dirsize = 0
$files = Get-ChildItem $_.directory -Recurse
foreach ($file in $files) {
	$dirsize += $file.length
}
$dirsize = [string]([math]::round(($dirsize / 1048576),2)) + " MB"
$Computer + "," + $Computer +"," +  $_.Fullname + "," + $_.Length + "," + $_.directory + "," + $files.count + "," + $dirsize + "," +$_.LastAccessTime }  |Out-File h:\$Filname –width 1024 

Open in new window

0
 

Author Comment

by:grayjfiii
ID: 39185307
Yes MB and GB if possible
0
 

Author Closing Comment

by:grayjfiii
ID: 39185388
Thank you for the fast response!
0

Featured Post

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question