VBScript Help - Handling errors

February 8, 2010 at 01:44:29
Specs: Windows XP
Hiya guys,

I've got a script that deletes files older than 14 days and empty folders but it seems to work if i have the delete statements commented out and its doesn't work with them. I found that its bugging out if the file is in use and gives me share violations is there any way to ignore the error and move on to the next file??

Here is some of my code,

Sub CleanupFiles(folderToClean)
	If CheckForSkip(folderToClean) Then Exit Sub

	dim objFile
	Set objFolder = fso.GetFolder(folderToClean)
	Set objFiles = objFolder.Files

	For Each objFile in objFiles
		If DateDiff("d", objFile.DateCreated, Now) > numberOfDays Then
			open_File.WriteLine ""& objFile.Path
			objFile.Delete
			count = count + 1
			
			
		End If
	Next

	Set objFolder = Nothing
	Set objFiles = Nothing
End Sub

Sub CleanupFolder(folderToCheck)
	If CheckForSkip(folderToCheck) Then Exit Sub

	Set objFolder = fso.GetFolder(folderToCheck)
	Set objSubfolders = objFolder.Subfolders
	Set objFiles = objFolder.Files

	If objFiles.Count = 0 and objSubfolders.Count = 0 Then
		objFolder.Delete
		
	End If

	Set objFolder = Nothing
	Set objSubfolders = Nothing
	Set objFiles = Nothing
End Sub



See More: VBScript Help - Handling errors

Report •


#1
February 8, 2010 at 05:24:05
At the start of your script, add the line:
On Error Resume Next


Report •

#2
February 8, 2010 at 06:45:50
I already have that... If seems to jump out of the for loop tho :(

Report •

#3
February 8, 2010 at 07:36:28
Apperently you don't have it in the right spot.
Also, this line should also break your script: open_File.WriteLine ""& objFile.Path

Report •

Related Solutions

#4
February 8, 2010 at 07:50:40
Why should it break it ?

This is my whole script:

Option Explicit
On Error Resume Next

Dim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheck, fts, foldersToSkip, skippedfolders, fsoObject, open_File, target_File, thisday, count 
Set fsoObject = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
Set fts = CreateObject("Scripting.Dictionary")

	PathToClean = "F:\GROUP\GENERAL"
	numberOfDays = 14
	foldersToSkip = "Job Descriptions;HSE Training Event Modules;HSE Training Info;HSE Training Matrix"
	count = 0
today_date()
	target_File = "C:\temp\CleanUp\" & thisday & ".txt"
Open_My_File()
	open_File.WriteLine "*******************************************"
	open_File.WriteLine "* Cleanup for: " & thisday & ".                *"
	open_File.WriteLine "* Run at: " & time & ".                    *"
	open_File.WriteLine "* This file is named: " & thisday & ".txt      *"
	open_File.writeline "*******************************************"
	open_File.WriteLine " "
	open_File.WriteLine " "

If Right(PathToClean, 2) = ":\" or Right(PathToClean, 1) = ":" Then

	open_File.WriteLine "Whoa Nelly!  It's best not to run the Cleanup on a drive root like " & PathToClean & "Don't Do That!"
End If

Set rootFolder = fso.GetFolder(PathToClean)
If Err.Number > 0 Then
	open_File.WriteLine PathToClean + " is not a valid directory path.  Please correct the path and run the script again. Path Not Found"
	Wscript.Quit
End If

EnumerateFoldersToSkip(foldersToSkip)
GetSubfolders(rootFolder)
CleanupFiles(rootFolder)

	open_File.WriteLine ""
	open_File.WriteLine "Files older than " & CStr(numberOfDays) & " days have been deleted from " & PathToClean
	open_File.WriteLine ""
	open_File.WriteLine "*********************************"
	open_File.WriteLine "* Cleanup Complete " & thisday & ".  *"
	open_File.WriteLine "* Finished at: " & time & ".     *"
	open_File.WriteLine "* File Count: " & count & ".          *"
	open_File.writeline "*********************************"


Set fso = Nothing

Wscript.Quit

Sub EnumerateFoldersToSkip(skippedfolders)
	If skippedfolders <> "" Then
		If InStr(1, skippedfolders, ";", 1) <> 0 Then
			Dim arrSkippedFolders, sf
			arrSkippedFolders = Split(skippedfolders, ";")
			For each sf In arrSkippedFolders
				fts.Add UCase(Trim(sf)), ""
			Next
		Else
			fts.Add UCase(skippedfolders), ""
		End If
	End If
End Sub

Sub GetSubfolders(folder)
	If CheckForSkip(folder) Then Exit Sub

	Dim oSubfolder
	Set objFolder = fso.GetFolder(folder)
	Set objSubfolders = objFolder.Subfolders

	For Each oSubfolder in objSubfolders
		If fts.Exists(UCase(oSubfolder.Name)) = False Then
			
			GetSubfolders(oSubfolder.Path)
	
			
			CleanupFiles(oSubfolder.Path)
	
			
			CleanupFolder(oSubfolder.Path)	
		End If
	Next
End Sub

Sub CleanupFiles(folderToClean)
	If CheckForSkip(folderToClean) Then Exit Sub

	dim objFile
	Set objFolder = fso.GetFolder(folderToClean)
	Set objFiles = objFolder.Files

	For Each objFile in objFiles
		If DateDiff("d", objFile.DateCreated, Now) > numberOfDays Then
			open_File.WriteLine ""& objFile.Path
			objFile.Delete
		if Err.Number <> 0 Then
			open_File.WriteLine "ERROR: " & Err.Number & " - " & Err.Description & " " & objFile.Path
		End If
			count = count + 1
		End If
	Next

	Set objFolder = Nothing
	Set objFiles = Nothing
End Sub

Sub CleanupFolder(folderToCheck)
	If CheckForSkip(folderToCheck) Then Exit Sub

	Set objFolder = fso.GetFolder(folderToCheck)
	Set objSubfolders = objFolder.Subfolders
	Set objFiles = objFolder.Files

	If objFiles.Count = 0 and objSubfolders.Count = 0 Then
		objFolder.Delete
		if Err.Number <> 0 Then
			open_File.WriteLine "ERROR - FOLDER: " & Err.Number & " - " & Err.Description & " " & objFolder.Path
		End If
	End If

	Set objFolder = Nothing
	Set objSubfolders = Nothing
	Set objFiles = Nothing
End Sub
Close_my_File()

Function CheckForSkip(folderToCheck)
	CheckForSkip = fso.FileExists(folderToCheck & "\CLEANUP.SKIP")
End Function


Function Open_My_File()

  If (fsoObject.FileExists(target_File)) Then
    Set open_File = fsoObject.OpenTextFile(target_File, 8)
  Else
    Set open_File = fsoObject.OpenTextFile(target_File, 2, "True")
  End If

End Function

Function Close_My_File()
  
  open_File.Close()

End Function

Function Today_Date()
	thisday= Right("0" & Day(Date),2) & "-" & Right("0" & Month(Date),2) & "-" & Right(Year(Date),4)
End Function



Report •

#5
February 8, 2010 at 10:08:30
On an error, control bubbles up to the lowest Sub/Function that has On Error Resume Next enabled. In your case, it's the global scope.

You need On Error Resume Next inside the Subs CleanupFiles and CleanupFolder. Additionally, the former Sub needs an Err.Clear in its error handling If statement.


Report •


Ask Question