Solved

Powershell script to delete files in a folder based on age

Posted on 2015-02-11
4
340 Views
Last Modified: 2015-02-13
I need a powershell script to perform the following:
1.  Determine the size a particular drive
2.  If free-space is less than say 10% then delete oldest .csv files from specific directories

Background:
System Event log files are archived as csv files for specific event types...Application, security, etc.  The path is something like e:\Saved_logs\Application;  e:\Saved_logs\Security; etc.  Each of these subfolders contain csv files that contain a servername-timestamp.csv file
(ex: servername-01012015-midnight.csv).  There may be 20 files covering 20 servers for a particular time frame.  The idea is, if the the freespace of the volume is less than 10% then search in each of these subfolders and delete the oldest files by date.
0
Comment
Question by:BigmacMc
  • 2
4 Comments
 
LVL 46

Expert Comment

by:noxcho
ID: 40605131
Get-ChildItem M:\ | Sort CreationTime | Select -Last 1 | Remove-Item

This way you can delete the oldest file if you have in this folder only the specific extension files.
Here Bryan made an excellent work: http://www.codeproject.com/Articles/564957/Clean-Up-Old-Files-using-PowerShell
Seems like exactly what you need.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40606876
There are several ways to approach this, and it is kind of open as you didn't say exactly which files should get deleted.

For example, we could go into a loop and delete the oldest file found in all folders, until free space is ok again.
[WMI] $disksize = "Win32_LogicalDisk.DeviceID='E:'".Size
$percfree = $part.FreeSpace/$part.Size * 100.0
while (([WMI] "Win32_LogicalDisk.DeviceID='E:'").FreeSpace / $disksize * 100.0 -lt 10.0)
{
  get-childitem -recurse 'E:\Saved_logs\' | ? { !$_.PsIsContainer } | sort CreationTime |
    select -First 1 | Remove-Item
}

Open in new window

Or we could just delete the oldest file of each folder.
[WMI] $part = "Win32_LogicalDisk.DeviceID='E:'"
if ($part.FreeSpace/$part.Size*100.0 -lt 10.0)
{
   foreach ($root in 'E:\Saved_logs\Application', 'E:\Saved_logs\Security')
   {
      get-childitem $root -recurse | sort CreationTime | select -First 1 | Remove-Item
   }
}

Open in new window

Note that you have to use -First, not -Last as shown by noxcho, to get the oldest file.
0
 

Author Comment

by:BigmacMc
ID: 40607921
Thanks for the feedback.  I'll try this out over the weekend.
0
 

Author Comment

by:BigmacMc
ID: 40609106
Qlemo - the 2nd one worked brilliantly.  This work for me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…

746 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

14 Experts available now in Live!

Get 1:1 Help Now