Solved

Powershell script to delete files in a folder based on age

Posted on 2015-02-11
4
358 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 69

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

792 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