Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript to delete files within a certain time

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,1200
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to delete files within a certain time
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 VBScript to delete files within a certain time - 7/30/2004 8:45:06 AM   
  supaben34

 

Posts: 1
Score: 0
Joined: 7/30/2004
From: USA
Status: offline
I started designing a script long ago that was supposed to help me create all filenames within a specific directory. My code works on deleting files on my hard drive just fine but when I try to delete files off of a removable disk such as a zip disk, it simply refuses to function. Please let me know what is wrong with my code Thank you.


      
 
 
Post #: 1
 
 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

(in reply to supaben34)
 
 
Post #: 2
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to delete files within a certain time Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts