Solved

Directory Size and File count

Posted on 2013-05-21
5
392 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
  • 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 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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