Solved

Powershell script to delete files in a folder based on age

Posted on 2015-02-11
4
371 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
[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
  • 2
4 Comments
 
LVL 47

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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:…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

756 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