Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript Logfile cleanup

Posted on 2003-04-01
5
Medium Priority
?
1,155 Views
Last Modified: 2007-12-19
The following script keeps popping up the following error:
Line: 140
Char: 1
Error: Object required: 'Folder'
Code: 800A01A8

I need this script to clean up backup directories after so many days. I don't know VBScript well so I need help, please.

Thanks,
Ross

[BeginScript]
Option Explicit

' Declare Variables
Dim wshArgs, wshShell
Dim FilePath, Retention, FileFilter

' Create wshArguments Object
Set wshArgs = wscript.arguments

' Create wshShell Object
Set wshShell = wscript.createobject("wscript.shell")

If ChkArgs = True then

' Get Arguments and Populate Variables
FilePath = wshArgs.named.item("dir")
Retention = wshArgs.named.item("days")

' Call Sub-Procedure to perform file deletion
DelFiles FilePath, Retention, FileFilter

' Display message stating that operation complete.
wscript.echo "Delete Operation Complete. Any problems encountered have been logged to the Application Log."
' Log Information Event in Application Log
wshShell.LogEvent 4, "Delete Operation Completed (CLEANUP.VBS)."

Else

' Display Usage Instructions
ShowUsage 1

' Log Warning in Event Log
wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS). Required Arguments Not Supplied"

' Display error message. When run as a scheduled task this message is suppressed.
wscript.echo "DELETE OPERATION FAILED!"
wscript.echo "The required Arguments have not been entered."

End If

' Clear Objects
Set wshArgs = Nothing
Set wshShell = Nothing
FilePath = ""
Retention = ""

'******************************************************************
' SUB-PROCEDURES AND FUNCTIONS
'******************************************************************

Function ChkArgs

' Declare Variables
Dim PathArg, DaysArg, ExtArg, HelpArg

' Count Number of Arguments Entered
PathArg = wshArgs.Named.exists("dir")
DaysArg = wshArgs.named.exists("days")
ExtArg = wshArgs.named.exists("ext")
HelpArg = wshArgs.named.exists("?")

' If all necessary arguments (2) entered Return True
' If not return False
' If the Help Argument (/?) is entered display Usage Instructions

If HelpArg = True then
ShowUsage 0
wscript.quit
End If

If PathArg = True and DaysArg = True then
ChkArgs = True
Else
ChkArgs = False
End If

If ExtArg = True then
FileFilter = True
Else
FileFilter = False
End If

End Function

Sub ShowUsage(Mode)

' Display Usage Instructions
wscript.echo "cleanup.vbs Usage Instructions:" & _
vbcrlf & _
vbcrlf & _
" cleanup.vbs /dir:[path] /days:[retention period] /ext:[file extension]" & _
vbcrlf & _
vbcrlf & _
"path = this is the full path to the directory to be cleaned" & _
vbcrlf & _
vbcrlf & _
"retention period = this is the length of time in days that files are to be kept" & _
vbcrlf & _
vbcrlf & _
"file extension = optional argument. Only files with specified extension will be deleted (enter WITHOUT a preceding dot)" & _
vbcrlf & _
vbcrlf & _
"Any files that are outside the specified retention period will be deleted."

' 0 = Help Only Mode. In this mode exit script after displaying usage instructions
' and do not attempt a delete operation
If Mode = 0 then
wscript.quit
Else
End If

End Sub

Sub DelFiles(Dir, Days, DelFilter)

' Procedure to cleanup old files

' Declare Variables
Dim DelDate, FSO, Folder, Files, File, FileDate, FilterExt, Ext

' Determine date six months ago by calculating todays date minus 182 days
DelDate = Now - Days

' Create Scripting Runtime FileSystemObject
Set FSO = createobject("scripting.filesystemobject")

' Create Folder Object
' If Path Not Found Generate Error and Event Log entry then exit script
On Error Resume Next
Set Folder = FSO.GetFolder(Dir)

If Err.Number = 76 then
wscript.echo "DELETE OPERATION FAILED! - Path Not Found."
wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS) - Path Not Found."
wscript.quit
End If
On Error Goto 0

' Create File Object
Set Files = Folder.Files

' For each file in the directory check the file age and if older than the specified
' retention period delete it
' If an extension filter was specified only delete if the extention matches as well
For each File in Files
FileDate = File.DateCreated

Ext = FSO.GetExtensionName(File)

On Error Resume Next

If DelFilter = True then
If FileDate <= DelDate and Ext = FilterExt then
File.Delete
End If
Else
If FileDate <= DelDate then
File.Delete
End If
End If

' Log error to Event Log and Screen if permission denied
If Err.Number = 70 Then
wscript.echo "Deletion of " & Dir & "\" & File.Name & _
" Failed - ACCESS DENIED!"
wshShell.LogEvent 2, "Deletion of " & Dir & "\" & _
File.Name & " Failed - ACCESS DENIED! (CLEANUP.VBS)"
End If

On Error Goto 0
Next

' Clear File and Folder Objects
Set Files = Nothing
Set Folder = Nothing
Set FSO = Nothing

End Sub
[EndScript]
0
Comment
Question by:bozo7
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:aeklund
ID: 8246814
To start, I would add some debug code... add this and see if you get an error message:  (it may be that it cannot set the Folder object due to a different error than error 76)

' Create Folder Object
' If Path Not Found Generate Error and Event Log entry then exit script
On Error Resume Next
Set Folder = FSO.GetFolder(Dir)

If Err.Number = 76 then
wscript.echo "DELETE OPERATION FAILED! - Path Not Found."
wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS) - Path Not Found."
wscript.quit
End If

'new code
If Err.Number <> 0 then
  wscript.echo err.Number & ": " & err.description
end if

On Error Goto 0

' Create File Object
Set Files = Folder.Files
0
 
LVL 1

Author Comment

by:bozo7
ID: 8246913
Good Call!
Now I get "424: Object Required"
The Folder object must not be getting intialized, correct?
Thanks,
Ross
0
 
LVL 10

Accepted Solution

by:
aeklund earned 2000 total points
ID: 8247030
Yup, sounds like it....  

Why don't you move that err checking code up a few lines to see if the folder exists, and also see what the dir string is set to:

' Create Scripting Runtime FileSystemObject
Set FSO = createobject("scripting.filesystemobject")

'just to see if the Dir variable is set
wscript.echo "Dir: " & Dir

If Not FSO.FolderExists(Dir) Then
  wscript.echo Dir & vbcrlf &  "Folder does not exist."
  wscript.echo "DELETE OPERATION FAILED! - Path Not Found."
  wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS) - Path Not Found."
  wscript.quit
End If

0
 
LVL 1

Author Comment

by:bozo7
ID: 8247141
The Dir was blank. I put another echo up where the args are captured. Blank up there also. That meant that the args were not being passed into the script. So I modified how I was calling the script and it works. User error.

Thank you for the great lesson in vb script,
Ross
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8247167
No problem... glad to help.

I have written many vbscripts in the past. <g>
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

564 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