Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Unable to catch error in function

$
0
0

Hello all, 

Lately I have decided to write some functions because I copy/paste a lot of my VBS code. Functions should make it easier to recycle written code, but I'm having troubles with catching errors in case anything goes wrong... 

The code inside the functions is what we used all the time in hundreds of scripts (always copy / pasted, without functions) so I'm absolutely positive that is correct and works, but when I place it inside a function, the function quits when anything goes wrong like deleting a folder from which a file is still in use. When I remove the start and the end of the function around the code, the error is caught and written to the log, but inside the function it quits and goes on with the rest of the scipt... 

We've been looking with a few colleagues for hours, without results. Hope you guys can tell me what we're doing wrong. 

A script without functions to delete the folder C:\Program Files\Alfa which works, whether you have a file in that folder in use or not. It will write the error to the log in that case: 

On error resume next
Dim objFS,strRunCmd,killErr,strErcode,WshShell,strInstallPath,strLogFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
AppData = WshShell.ExpandEnvironmentStrings("%APPDATA%")
UserName = wshShell.ExpandEnvironmentStrings("%USERNAME%")
Set objNet = WScript.CreateObject("Wscript.NetWork")
strComputerName = objNet.ComputerName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

exitcode = 0

'Log name
Set strLogFile = objFS.CreateTextFile("C:\Windows\Temp\Alfa_Adsuite_2013_2.log", True,False)

'Log info
strInstallPath = objFS.GetFile(WScript.ScriptFullName).Parentfolder
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("************** Alfa Adsuite 2013.2 ****************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteBlankLines(1)
strLogfile.WriteLine(" - Install path = " & strInstallPath)
strLogfile.WriteLine(" - Time = " & now)
strLogfile.WriteBlankLines(2)

'Determine OS Type
If (instr(osType,"64")) Then
	processor = "x64" 
	pf = "Program Files (x86)"
	sysfolder = "SysWOW64"
	strLogfile.WriteLine("Processor architecture = " & osType) 
	strLogfile.WriteLine("--> 64-bit Windows found <--") 
	strLogfile.WriteBlankLines(2)
Else
	processor = "x86"
	pf = "Program Files"
	sysfolder = "System32"
	strLogfile.WriteLine("Processor architecture = " & osType) 
	strLogfile.WriteLine("--> 32-bit Windows found <--")
	strLogfile.WriteBlankLines(2)
End If

'''''''''''''''
''' INSTALL '''
'''''''''''''''

strFolderName = "C:\Program Files\Alfa"
strNumber = 10

'Delete Alfa folder
strLogfile.WriteLine("*** Delete folder " & strFolderName & " for max " & strNumber & " times - " & now)
strLogfile.WriteBlankLines(1)
FolderFound = 1
Index = 1
IndexEnd = strNumber

'Try X times to delete folder
If objFS.FolderExists (strFolderName) Then
	Do While FolderFound = 1 And Index < IndexEnd + 1
		strLogfile.WriteLine(" * Attempt " & Index)
		Err.Clear
		objFS.DeleteFolder (strFolderName)
		If Err.Number <> 0 Then 
			FolderFound = 1
			strLogfile.WriteLine(" - Unable to delete " & strFolderName & ": " & Err.Description)
			strLogfile.WriteBlankLines(1)
			Err.Clear
			wscript.sleep 10000
		Else
			FolderFound = 0
			strLogfile.WriteLine(" + Folder " & strFolderName & " deleted")
			strLogFile.WriteBlankLines(1)
		End If

		Index = Index + 1
	Loop
	'Check if folder is gone, otherwise count as error
	If objFS.FolderExists (strFolderName) Then
		exitcode = exitcode + 1
	End If
Else
	strLogfile.WriteLine(" * " & strFolderName & " not found")
	strLogfile.WriteBlankLines(1)
End If

'End Script
strLogfile.WriteBlankLines(1)
StrLogfile.WriteLine("Exitcode = " & exitcode & " - " & now)

strLogFile.Close

wscript.quit(exitcode)

A script with a function to delete the folder C:\Program Files\Alfa which DOES NOT WORK when you have a file in that folder in use but which DOES WORK when no files are in use, you can try it for yourself:

On error resume next
Dim objFS,strRunCmd,killErr,strErcode,WshShell,strInstallPath,strLogFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
AppData = WshShell.ExpandEnvironmentStrings("%APPDATA%")
UserName = wshShell.ExpandEnvironmentStrings("%USERNAME%")
Set objNet = WScript.CreateObject("Wscript.NetWork")
strComputerName = objNet.ComputerName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

exitcode = 0

'Log name
Set strLogFile = objFS.CreateTextFile("C:\Windows\Temp\Alfa_Adsuite_2013_2.log", True,False)

'Log info
strInstallPath = objFS.GetFile(WScript.ScriptFullName).Parentfolder
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("************** Alfa Adsuite 2013.2 ****************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteLine("***************************************************")
strLogfile.WriteBlankLines(1)
strLogfile.WriteLine(" - Install path = " & strInstallPath)
strLogfile.WriteLine(" - Time = " & now)
strLogfile.WriteBlankLines(2)

'Determine OS Type
If (instr(osType,"64")) Then
	processor = "x64" 
	pf = "Program Files (x86)"
	sysfolder = "SysWOW64"
	strLogfile.WriteLine("Processor architecture = " & osType) 
	strLogfile.WriteLine("--> 64-bit Windows found <--") 
	strLogfile.WriteBlankLines(2)
Else
	processor = "x86"
	pf = "Program Files"
	sysfolder = "System32"
	strLogfile.WriteLine("Processor architecture = " & osType) 
	strLogfile.WriteLine("--> 32-bit Windows found <--")
	strLogfile.WriteBlankLines(2)
End If

'''''''''''''''
''' INSTALL '''
'''''''''''''''

'Delete Alfa folder
delete_alfa = fn_delete_folder_X_attempts ("C:\Program Files\Alfa",10)
StrLogfile.WriteLine("--> Delete Alfa folder functioncode = " & delete_alfa)
strLogfile.WriteBlankLines(1)

'End Script
strLogfile.WriteBlankLines(1)
StrLogfile.WriteLine("Exitcode = " & exitcode & " - " & now)

strLogFile.Close

wscript.quit(exitcode)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' FUNCTIONS - DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING !!! '''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' INDEX ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' fn_delete_folder_X_attempts
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'---------------------------------------------------------------------------------
'*** fn_delete_folder_X_attempts - Delete a folder, retry X times when failing ***
'---------------------------------------------------------------------------------
'EXAMPLE
'Folder_B = fn_delete_folder_X_attempts ("C:\Folder_A\Folder_B",5)
'-----------------------------------------------------------------
Function fn_delete_folder_X_attempts (strFolderName,strNumber)
	strLogfile.WriteLine("*** Delete folder " & strFolderName & " for max " & strNumber & " times - " & now)
	strLogfile.WriteBlankLines(1)
	fn_delete_folder_X_attempts_code = 0
	FolderFound = 1
	Index = 1
	IndexEnd = strNumber
	'Try X times to delete folder
	If objFS.FolderExists (strFolderName) Then
		Do While FolderFound = 1 And Index < IndexEnd + 1
			strLogfile.WriteLine(" * Attempt " & Index)
			Err.Clear
			objFS.DeleteFolder (strFolderName)
			If Err.Number <> 0 Then 
				FolderFound = 1
				strLogfile.WriteLine(" - Unable to delete " & strFolderName & ": " & Err.Description)
				strLogfile.WriteBlankLines(1)
				Err.Clear
				wscript.sleep 10000
			Else
				FolderFound = 0
				strLogfile.WriteLine(" + Folder " & strFolderName & " deleted")
				strLogFile.WriteBlankLines(1)
			End If

			Index = Index + 1
		Loop
		'Check if folder is gone, otherwise count as error
		If objFS.FolderExists (strFolderName) Then
			exitcode = exitcode + 1
			fn_delete_folder_X_attempts_code = fn_delete_folder_X_attempts_code + 1
		End If
	Else
		strLogfile.WriteLine(" * " & strFolderName & " not found")
		strLogfile.WriteBlankLines(1)
	End If

	Err.Clear
	
	'Function result
	fn_delete_folder_X_attempts = fn_delete_folder_X_attempts_code
End Function




Viewing all articles
Browse latest Browse all 15028

Trending Articles