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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

also some helpful examples on this website -->
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SupermanTBAuthor Commented:
Thanks very much.  I'm going to test this out.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.