We help IT Professionals succeed at work.

Looking for an automated script to delete files after a 24 hour period

524 Views
Last Modified: 2013-11-05
I am looking to create an automated script for our company FTP site that will delete files that are created in a folder after a 24 hour period. I would like it to be done in VB Script as I am kind of familiar with it. I have been searching the web and have been having some trouble with this. It seems like an easy request. ANy help on this topic would be greatly appreciated.

Thank you so kindly in advanced.
Comment
Watch Question

Commented:
You can use the filesystemobject for this task in vbscript:


Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

objFSO.DeleteFile "C:\Temp\*.*", False

Set objFSO = Nothing
CERTIFIED EXPERT
Top Expert 2007

Commented:
      Hi jeremy
            Please have a look at following
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21972541.html

Regards

Commented:
That deletes all the files.  Was that what you were looking for, or were you planning to do some filtering?

Author

Commented:
I need to filter by the date created. But you are on the right track. I would basically need it to be 24 hours from the date created. Can it be done?
CERTIFIED EXPERT
Top Expert 2006

Commented:
Run the follwing vbscript using CScript Thisfile.vbs. It would display the files that would be deleted. If the list is correct, uncomment the line 'Call objFile.Delete(True) by removing the ' from the line and run again. The files would be deleted.

Dim objFSO, objFolder, objFiles, objFile
Dim DateTimeToDelete
Dim FilesLocation

FilesLocation = "C:\WhateverFolder\SubFolder"

Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(FilesLocation)
Set objFiles = objFolder.Files

DateTimeToDelete = Now() - 1

Wscript.Echo DateTimeToDelete

For Each objFile In objFiles
      If (objFile.DateLastModified >= DateTimeToDelete) Then
            Wscript.Echo "Delete " & objFile.Name & " : " & objFile.DateLastModified
                'Call objFile.Delete(True)
      End If
Next

Set objFile = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Barry62......What if I wanted it to be less then 24 hours could I just change the 24 to what ever time frame needed? Lets say 8 hours?
CERTIFIED EXPERT
Top Expert 2006

Commented:
Dim objFSO, objFolder, objFiles, objFile
Dim DateTimeToDelete
Dim FilesLocation

FilesLocation = "C:\WhateverFolder\SubFolder"

Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(FilesLocation)
Set objFiles = objFolder.Files

'Change 24 to whatever hrs
DateTimeToDelete = DateAdd("h", -24, Now())

Wscript.Echo DateTimeToDelete

For Each objFile In objFiles
      If (objFile.DateLastModified <= DateTimeToDelete) Then
            Wscript.Echo "Delete " & objFile.Name & " : " & objFile.DateLastModified
            'Call objFile.Delete(True)
      End If
Next

Set objFile = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

Author

Commented:
Barry62....When I run your script, it works but I get the followiing error:

Line 8
Char 2
Permission Denied
Code: 800A0046

Author

Commented:
amit_g...How do I stoop the popups? When I run the script it doesn't delete anything, I changed the 24 to a 1 for testing purposes. Can you help?

Commented:
You need to give modify permissions to the IUSR group in order to delete files.
CERTIFIED EXPERT
Top Expert 2006

Commented:
You must be running it by double-cliking it. While testing, you should use command promt. Open a command prompt (DOS window) by Start->Run->cmd. Then cd to folder where you have this script and then run

CScript ThisScript.vbs

I have commented the statement to delete.

'Call objFile.Delete(True)

when you have tested ut carefully, you can remove the WScript.Echo lines and remove the comment (remove ' from the above line) and the script would delete.

Commented:
let me clarify...

You need to give modify permissions to IUSR only in the folder where you will be deleting files.

Author

Commented:
This is what I get when run from command prompt:

C:\>cscript dailydelete2.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

8/14/2007 1:15:23 PM
Delete Perflib_Perfdata_a40.dat : 8/14/2007 8:31:56 AM
Delete wmsetup.log : 8/13/2007 9:50:24 AM
Delete ~DFCB80.tmp : 8/14/2007 8:33:39 AM
CERTIFIED EXPERT
Top Expert 2006

Commented:
This is a list of files that qulify to be deleted. Is this correct? If so, the script is working fine. Change

'Call objFile.Delete(True)

to

Call objFile.Delete(True)

and run again. These files would be deleted. Also remove all lines that start with WScript.Echo in the script to remove the debug information being printed.

Author

Commented:
Barry62....Just updated permissions and still getting error. Funny though because the script works great.

Author

Commented:
Amit_g.....ran script after doing what you said and here is what I got:

C:\>cscript dailydelete2.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\dailydelete2.vbs(19, 13) Microsoft VBScript runtime error: Permission denied


C:\>

Commented:
try changing the 'everyone' group for modify permissions.

and, yes, you just change the time period by changing '24' to whatever number of hours you want.  If you want days, change the "h" to "d".

CERTIFIED EXPERT
Top Expert 2006

Commented:
~DFCB80.tmp seems line a temp file being used from another process. You won't be able to delete this.

Author

Commented:
Barry62....Changed the permissions as you said and still the same error and also now it is not deleting the file anymore. Maybe soomething on my end. I will work with it. The permission thing worries me more.
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Well, change back to the permission settings as before, and do as amit_g says.  Add an On Error Resume Next statement.

Author

Commented:
How would I change this script to delete subfolders as well?
CERTIFIED EXPERT
Top Expert 2006

Commented:
'On Error Resume Next

Dim objFSO, objFolder, objSubFolders, objSubFolder, objFiles, objFile
Dim DateTimeToDelete
Dim FilesLocation

FilesLocation = "C:\WhateverFolder\SubFolder"

Set objFSO = CreateObject("Scripting.fileSystemObject")

Sub DeleteFilesInFolder(FilesLocation)
      Set objFolder = objFSO.GetFolder(FilesLocation)
      Set objSubFolders = objFolder.SubFolders
      
      For Each objSubFolder In objSubFolders
            DeleteFilesInFolder(FilesLocation & "\" & objSubFolder.Name)
            
            If (objSubFolder.SubFolders.Count = 0) And (objSubFolder.Files.Count = 0) Then
                  Call objSubFolder.Delete()
            End If
      Next
      
      Set objFiles = objFolder.Files

      'Change 24 to whatever hrs
      DateTimeToDelete = DateAdd("h", -24, Now())

      Wscript.Echo DateTimeToDelete

      For Each objFile In objFiles
            If (objFile.DateLastModified <= DateTimeToDelete) Then
                Call objFile.Delete(True)

                If Err.Number <> 0 Then
                    Wscript.Echo objFile.Name & " could not be deleted due to some error. " & Err.Description
                    Err.Clear
                Else
                    Wscript.Echo objFile.Name & " deleted."
                End If
            End If
      Next

      Set objFile = Nothing
      Set objFiles = Nothing
      Set objFolder = Nothing
End Sub

Set objFSO = Nothing

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.