Login | |
|
 |
Re: VBScript to delete files within a certain time - 9/10/2004 6:24:37 AM
|
|
 |
|
| |
cliff2_2
Posts: 1
Score: 0
Joined: 9/10/2004
From: USA
Status: offline
|
I changed this dramatically to do a delete by date only. The purpose of this was to clear old log files off of a mail server. Its short and sweet and works. I have seen alot of posts asking for how this is to be done. This can be easily modified with IF or Case statements to exclude certian file extensions. ' Program Description: This file deletes files from specified directory and all subdirectories under it that are older than 10 days. Option Explicit Dim FSO, WshShell, Then_Date, WSHShellENV, TempFolder, Recycled On Error Resume Next Set FSO = CreateObject("Scripting.FileSystemObject") ' creating a new object to a computer's file system Set WshShell = WScript.CreateObject("WScript.Shell") ' creates an object in type specified Set WSHShellENV = WSHShell.Environment("PROCESS") set TempFolder = FSO.GetFolder(WSHShellENV("TEMP")) ' creates a Folder object to the path you specify Set Recycled = FSO.GetFolder("C:\test") ' creates a Folder object to the path youspecify and name it DoDir FSO.GetFolder("C:\deleting") 'set starting directory A MUST Then_Date = DateAdd("d", -10, Date) Sub DoDir(Folder) Dim i, File, SubFolder, fstr, pos, last_mod ' creating variables needed for function Dim Now_Date, Then_Date ' creates the variable used for today's date Now_Date = Date ' loads today's date into variable Now_Date Then_Date = DateAdd("d",-10, Now_Date) ' subtracts ten days from the current date ' WScript.Echo(Then_Date) ' TEST FOR THEN DATE ' WScript.Echo(Now_Date) ' TEST FOR TODAY'S DATE ' WScript.Echo(Folder) ' TEST FOR CORRECT FOLDER For Each File In Folder.Files ' For each file in folder.files if ((File.DateLastModified <= Then_Date)) then ' check whether the file IS ' older than keep date File.delete ' if older than keep date, simply delete it End if Next For Each SubFolder in Folder.SubFolders DoDir SubFolder Next End Sub
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|