Ryan S
Ryan S

Reputation: 3

File Size with Powershell

What I am trying to do is create a PS script to see when a certain folder has a file over 1GB. If it found a file over 1GB, I want it to write a log file with info saying the name of the certain file and its size.

This works but not fully, if the file is less than 1GB I don't want a log file. (right now this will display the file info for over 1GB but if its less that 1GB it still creates a log file with no data). I don't want it to create a log for anything less than 1GB.

Any idea on how to do that?

Thanks!

Ryan

Get-ChildItem -Path C:\Tomcat6.0.20\logs -File -Recurse -ErrorAction SilentlyContinue | 
Where-Object {$_.Length -gt 1GB} | 
Sort-Object length -Descending | 
Select-Object Name,@{n='GB';e={"{0:N2}" -F ($_.length/ 1GB)}} | 
Format-List Name,Directory,GB > C:\Users\jensen\Desktop\FolderSize\filesize.log`

Upvotes: 0

Views: 3283

Answers (2)

Ben Personick
Ben Personick

Reputation: 3264

To make sure you don't write blank files you should collect the minimal starting results that match your filter, and test them to see if they contain anything at all.

Then if they on;t you can ed the script, but if they do you can go on to do the sort and select the data and output it to a log file.

# Collect Matching Files
$Matched = GCI -Path "C:\Tomcat6.0.20\logs" -File -R -ErrorA "SilentlyContinue" | ? {
  $_.Length -gt 1GB
}

#  Check is $Matched contains Results before further processing, otherwise, we're done!

IF ([bool]$Matched) {
  # If here, we have Data so select what we need and output to the log file:
  $Matched | Sort Length -D | FT Name,Directory,@{
    n="GB";e={"{0:N2}" -F ($_.Length/ 1GB)}
  } -Auto | Out-File "C:\Users\jensen\Desktop\FolderSize\filesize_$(Get-Date -F "yyyy-MM-dd_HH-mm-ss").log"
}

In the above script, I fixed the $. to be $_., and separated Matching the 1GB files from Manipulating them, and Outputting them to a file.

We simply test if any files were matched at 1 GB by checking to see if the Variable has any results or is $NULL/undefined.

If so, there is no need to take any further action.

Only when 1Gb files are matched do we quickly sort them, and select the details you wanted, but instead we'll just Use Format-Table (FT) with -Auto-size to get nice looking output that is much easier to review for this sort of data.

(Note Format-Table selects and formats the info into a table in one step, saving the unnecessary step of using Select to get the data and then piping (|) it to Format-list or Format-Table, as that just adds a bit of a redundant step. Select-Object is best used when you will need to do further steps with that data that require "object-oriented" operations in future steps.)

Then we pipe that output to save it all to a Log file using Out-File, and I also changed the log file name to contain the current date and time in ISO format filesize_$(Get-Date -F "yyyy-MM-dd_HH-mm-ss").log So you can save each run and review them later and won't want to have one gigantic file or have no history of runs.

Upvotes: 0

trebleCode
trebleCode

Reputation: 2308

First, set a variable with the term/filter you're after and store the results

$items = Get-ChildItem -Path C:\Tomcat6.0.20\logs -File -Recurse -ErrorAction SilentlyContinue |
     Where-Object {$_.Length -gt 1GB} | 
     Sort-Object Length -Descending |
     Select-Object Name,@{n='GB';e={"{0:N2}" -F ($_.length/ 1GB)}} 

Then pipe that to Out-File to your desired output path. This example will output a file to the Desktop of the user running the script, change as needed:

$items | Out-File -FilePath $ENV:USERPROFILE\Desktop\filesize.log -Force

The -Force parameter will overwrite an existing filesize.log file if one already exists.

Upvotes: 1

Related Questions