?
Solved

VBScript Logfile cleanup

Posted on 2003-04-01
5
Medium Priority
?
1,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 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