Blue Flower

How To Automatically Have Things Happen on Windows XP (Such as delete Files, and Folder)

The following can be slightly modified to make almost any kind of task or job done automatically and/or on a scheduled basis using Windows Task Manager called Scheduled Tasks in Windows XP Operating systems.  Maybe same or similar in other Windows operating systems.

Please note that I do not get paid to do these articles.

Quick Summary Info.

This is Visual Basic code (.vbs), and it will execute (operate) as part of Windows Script Host (usually associated with a .wsf).  You can latterly copy the following into a Notepad and save it as a .vbs file, and it should work.
This example removes the unwanted (IMO) Firefox Crash Report Pending Folder content (files) when older than 2 days.  That folder can collect old unwanted reports, regardless of your Firefox setting, and grow to be many Gigabytes of space if not attended to.

 ' **** Declarations
Option Explicit
On Error Resume Next
Dim oFSO, oFolder, sPathNMainFolder
Dim oFilesInFolder, oFile, sDir
Dim iFileDaysOld

iFileDaysOld = 2

' **** To Clean Main Folder
sPathNMainFolder = "C:\Documents and Settings\Owner\Application Data\Mozilla\Firefox\Crash Reports\pending\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPathNMainFolder)
Set oFilesInFolder = oFolder.Files

For each oFile in oFilesInFolder
     If oFile.DateLastModified < (Date() - iFileDaysOld) Then
         oFile.Delete(True)
     End If
Next

' **** To Clean Subfolders

For Each oSubFolder In oSubFolders
    sPathNMainFolder = "C:\Documents and Settings\Owner\Application Data\Mozilla\Firefox\Crash Reports\pending\" & oSubFolder
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(sPathNMainFolder)
    Set oFilesInFolder = oFolder.Files

    For each oFile in oFilesInFolder
         If oFile.DateLastModified < (Date() - iFileDaysOld) Then
             oFile.Delete(True)
         End If
    Next

    If oSubFolder.Size = 0 Then oSubFolder.Delete(True)
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFilesInFolder = Nothing
    Set oFile = Nothing
Next

' **** Cleanup of Script Usage
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFilesInFolder = Nothing
    Set oFile = Nothing

    WScript.Quit

 

One change you may need to do is change the "\Owner\" to the profile name (correct path name) used on your PC, in case it is not Owner, which it often is.

You can change "C:\Documents and Settings\Owner\Application Data\Mozilla\Firefox\Crash Reports\pending\" to any other folder path that you want content and subfolder checked on.

You can simply open any text editor like Notepad and copy and paste the above into the editor and save the file as something like TechNotesCleanup.vbs.  Lets say you save it as this in the My Document folder.

Then do the following.

 Go to Start >> All Programs >> Accessories >> System Tools >> Scheduled Tasks

Go to Start >> All Programs >> Accessories >> System Tools >> Scheduled Tasks

 

Open Add Scheduled Task

Select Add Scheduled Task

 

Choose Next

Select Next on Scheduled Task Wizard

 

 Choose Browse

Choose Browse on Scheduled Task Wizard

 

Open TechNotesCleanup.vbs

 

Select the time period or running (executing) the cleanup script. In this case, daily has been chosen.  Push Next after selecting the period for task schedule.
Choose Period on Scheduled Task Wizard

 

Set the Start Time, Start Date, and how often this task will recur, and push Next.

Choose Time, Date and period on Scheduled Task Wizard

 

Next leave the User Name as is, unless you want the task to operating under a different Username.  You can either leave the password blank or enter it here.  It will need the password to be able to run the task, or you can leave it blank and on a later window select for the task to operate only when you are logged into the PC, which is what I do. Push Next.

Enter username and password on Scheduled Task Wizard

 

Check the box to" Open Advanced properties", and click on Finish.

Select Advanced properties on Scheduled Task Wizard

 

You will probably see a warning message like this, but that is okay, and so go ahead and click on OK button. This message shows due to not entering the password, and thus the Task Schedule does not get access.

Task Scheduler warning

 

On a new window that shows, check the box that is for "Run only if logged on"

Task window settings

 

Select the Settings tab, and change the "Stop the task if it runs for" to be your preference.  I think 1 hour is good, and that is what this example shows.   Click OK button.

Settings of scheduled task

 

On the Scheduled Tasks window right mouse button click on the task named TechNotesCleanup, and choose Run.

Run Task To Test It

 

If you get a windows or firewall software security warning message about running an unknown script, go ahead and allow it and make it an exception so it can always run without question.  Here is only one example.  The message or window that shows, if any will depend on your Virus or Malware or Firewall software brand, operation, and version.

 

If no error message shows, then that means you pasted the code in correctly without hidden formatting characters to cause problems and that the folder path you indicated correctly exists.

 

References:

www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/wsh_runfromwindowsbasedhost.mspx?mfr=true
ss64.com/vb/
www.makeuseof.com/tag/clean-temp-files-windows-script