Remove old files in a directory

Posted on 2011-05-09
Last Modified: 2012-05-11

I have a server that I do backups on, I am wondering if there is an easy script or program that can be used for clearing out the oldest files in the directory by deletion.
Question by:spiderwilk007
    LVL 92

    Expert Comment

    by:Patrick Matthews

    1) Does the script have to remove files from subordinate directories as well?

    2) Is there a rule to follow, such as "kill everything with DateLastModified 30 or more days ago"?

    LVL 51

    Expert Comment

    by:Bill Prew
    LVL 8

    Author Comment

    No subordinate folders to worry about, it's a single directory, I want the script to kill anything older than 2 weeks.
    LVL 8

    Accepted Solution

    I use the attached script to delete any webserver log files older than 14 days.

    I just have a sceduled task start a cleanup.bat file and log what its deleted.
    If you edit lines 3 and 11 for the path and file type (if needed)

    REM cleanup.bat
    cscript.exe //nologo cleanlog.vbs >>cleanlog.txt

    wscript.echo "Script Started: " & date & ", " & Time
    Dim fso, f, f1, fc, strComments, strScanDir
    strDir = "C:\WINDOWS\system32\LogFiles\W3SVC1"
    strDays = 14
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(strDir)
    Set fc = f.files
    For Each f1 in fc
       if right(,4)=".log" then 
          If DateDiff("d", f1.DateCreated, Date)> strDays Then
                strComments = & " " & f1.DateCreated
    wscript.echo strComments
          End If
       end if
    wscript.echo "Script Finished: " & date & ", " & Time

    Open in new window

    LVL 15

    Expert Comment

    Here is a script I use that also creates a timestamped circular log file of which files were deleted.  The only thing you need to change is the strPath variable.
    DIM strPath
    DIM Folder
    DIM Files
    DIM File
    DIM List
    DIM intDiff
    strPath = "C:\"
    set FSO = CreateObject("Scripting.FileSystemObject")
    set Folder = FSO.GetFolder(strPath)
    set Files = Folder.Files
    List = func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
    List = List & func_TimeStamp() & vbtab & "Beginning Progress Cleanup" & vbcrlf
    List = List & func_TimeStamp() & vbtab & FormatDateTime(Now(), 1) & vbcrlf
    List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
    For each File in Files
    	intDiff = DateDiff("D", File.DateLastModified, Now())
    	If intDiff > 30 then
    		List = List & func_TimeStamp() & vbtab & File.Path & vbcrlf
    	End if
    List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
    List = List & func_TimeStamp() & vbtab & "Ending Progress Cleanup" & vbcrlf
    List = List & func_TimeStamp() & vbtab & FormatDateTime(Now(), 1) & vbcrlf
    List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
    set strPath = Nothing
    set FSO = Nothing
    set Folder = Nothing
    set Files = Nothing
    set File = Nothing
    set List = Nothing
    set intDiff = Nothing
    Function func_TimeStamp()
    	DIM strYear
    	DIM strMonth
    	DIM strDay
    	DIM strTime
    	DIM strStamp
    	strYear = Year(Now)
    	strMonth = Month(Now)
    	strDay = Day(Now)
    	strTime = Time()
    	If len(strMonth) < 2 then
    		strMonth = "0" & strMonth
    	End If
    	If len(strDay) < 2 then
    		strDay = "0" & strDay
    	End If
    	strStamp = strYear & "-" & strMonth & "-" & strDay & " " & strTime
    	if len(strStamp) <> 22 then
    		Do Until len(strStamp) > 21
    			strStamp = strStamp & " "
    	end If
    	func_TimeStamp = strStamp
    	set strYear = Nothing
    	set strMonth = Nothing
    	set strDay = Nothing
    	set strTime = Nothing
    	set strStamp = Nothing
    End Function 'func_TimeStamp()
    Function func_Log(strLMSG)
    	DIM strFol
    	DIM strFile
    	DIM wFile
    	DIM rFile
    	DIM rLine
    	DIM rLog
    	DIM rDate
    	DIM nTime
    	strFold = "C:\ScriptLogs\"
    	strFile = strFold & "TRM001_Cleanup.txt"
    	Timestamp = func_TimeStamp()
            If FSO.FolderExists(strFold) = false then
            End If
    	If FSO.FileExists(strFile) then
    		Set rFile = FSO.OpenTextFile(strFile)
    		Do Until rFile.AtEndOfStream
    			rLine = rFile.ReadLine
    			If rLine <> "" then
    				rDate = CDate(split(rLine, vbtab)(0))
    				nTime = Now()
    				If DateDiff("D", rDate, nTime) < 90 then
    					rLog = rLog & rLine & vbcrlf
    				End If
    			End If
    	End If 
    	set wFile = FSO.OpenTextFile(strFile, 2, True)
    	wFile.write strLMSG & vbcrlf
    	wFile.Write rLog
    	set strFold = Nothing
    	set strFile = Nothing
    	set wFile = Nothing
    	set rFile = Nothing
    	set rLine = Nothing
    	set rLog = Nothing
    	set rDate = Nothing
    	set nTime = Nothing
    End Function 'func_Log()

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Problem description :  Some external hard disks / USB flash drives do not show actual space as mentioned in the factory settings. This is a common problem when you use an 8 GB USB drive to make it bootable to install a firmware/ driver on a serv…
    How to update Firmware and Bios in Dell Equalogic PS6000 Arrays and Hard Disks firmware update.
    This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
    This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now