Solved

Powershell script to delete files in a folder based on age

Posted on 2015-02-11
4
374 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 70

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

752 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