Solved

Directory Size and File count

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now