Solved

Directory Size and File count

Posted on 2013-05-21
5
410 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 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