Script to delete the oldest file in a folder

I've never really written Windows scripts before, although I do have a programming background.  What I'd like to do is write a script that will delete the oldest file (according to the date modified) in a particular folder once a day.  There will not be any more than 5 files in this particular folder at any given time.  

This script will be run in Windows Server 2008 R2.

Can someone offer a little assistance here?  Thanks very much.
Who is Participating?
oBdAConnect With a Mentor Commented:
Plain batch (save as Whatever.cmd); change the variables Folder and FileMask to your likings.
Note that the script is currently in test mode and will only display the "del" command it would normally run; remove the uppercase ECHO in line 14 to run it for real:
@echo off
set Folder=C:\temp
set FileMask=*.txt
set OldestFile=
for /f "delims=" %%a in ('dir /b /o:d "%Folder%\%FileMask%" 2^>NUL') do (
	set OldestFile=%%a
	goto Break
if "%OldestFile%"=="" (
	echo No files found in '%Folder%' matching '%FileMask%'!
) else (
	ECHO del "%Folder%\%OldestFile%"

Open in new window

You can use the FOR Files function - See -->

also some helpful examples on this website -->
SupermanTBAuthor Commented:
Thanks very much.  I'm going to test this out.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

SupermanTBAuthor Commented:
Worked great!  Thanks very much!
Bill PrewCommented:
Here's a VBS script approach in case you are interested.  Since you are new to scripting I tried to "over comment" a bit.  Save as a VBS file, adjust the file name at the top, and then run from a command line or BAT like:

cscript ee28272393.vbs

Right now it doesn't display any information about the file deleted, but that could be easily added if desired.

' Define locations of files to work with
strBaseDir = "c:\temp"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Access base files folder
Set objFolder = objFSO.GetFolder(strBaseDir)

' Initialize date for loop below to find oldest file
datOldest = #1/1/2100#

' Look at each file in base folder
For Each objFile In objFolder.Files

   ' See if this is older than any we have found so far
   If objFile.DateLastModified < datOldest Then

      ' If older, save its date and name as the oldest up to now
      datOldest = objFile.DateLastModified
      strOldest = objFile.Path

   End If


' Delete the oldest file
If strOldest <> "" Then
   objFSO.DeleteFile strOldest
End If

Open in new window

SupermanTBAuthor Commented:
billprew, thanks so much for that.  I really appreciate the commenting.  It really makes the code clear.

I had already awarded points before you posted your comment.  I'm sorry I don't have any points to award you.
Bill PrewCommented:
Not a problem on points, if it turns out to be useful for you that's all that matters.  I did borrow the code from a similar question, which was looking for the newest file, so didn't take too much effort.

Good luck,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.