Vbscript help

Soldato
Joined
18 Oct 2002
Posts
7,492
Location
Maidenhead
Ok so I needed something which can be run on a schedule which will copy a file to a remote pc, check it got there ok, then delete the old copy and rename the new copy (we are having network issues so finding incomplete files when transferring).

I have limited programming knowledge so decided to try vbscript. I have it working ok, but decided to try to split it down to use a class in order to add error handling as vbscripts error handling is limited.

Code:
Option Explicit
On Error Resume Next 

Set c = New MyClass
WScript.Echo "new class called"
c.MainSub
WScript.Echo "finished mainsub"

Class MyClass
	
	Dim FSO
	Dim LogFile
	Dim DestinationFile
	Dim strLine
	Dim strSourceFileName1
	Dim strSourceFullPath1
	Dim strTempDestFileName1
	Dim strTempDestFullPath1
	Dim strFinaltempdest1
	Dim source1
	Dim tempdest1
	Dim strSourceFileName2
	Dim strSourceFullPath2
	Dim strTempDestFileName2
	Dim strTempDestFullPath2
	Dim strFinaltempdest2
	Dim source2
	Dim tempdest2
	
	Sub Class_Initialize
		WScript.Echo "Initialise"
		Set FSO = CreateObject("Scripting.FileSystemObject")
		OpenLogFile
	End Sub
	
	Sub Class_Terminate
		WScript.Echo "terminating"
		Set FSO = Nothing
		LogFile.Close
		DestinationFile.Close
		
		WScript.Echo "Complete"
		WScript.Quit
	End Sub
	
	Public Sub MainSub()    
		WScript.Echo "Entered main sub"
		OtherSub myargs
		WScript.Echo "Exited othersub"
		' call other subs and functions that use fso
		' or use fso here
	End Sub
	
	Public Sub OpenLogFile()    
		WScript.Echo "Opening log file"
		Const ForWriting = 2, ForAppending = 8
		'Create objects
		
		'Check log file exists
		If (FSO.FileExists("Log.txt")) Then
			Set LogFile = FSO.OpenTextFile("Log.txt", ForAppending, True)
		Else
			Set LogFile = FSO.OpenTextFile("Log.txt", ForWriting, True)
		End If
		
		'Write starting to log
		LogFile.WriteLine("=========================================")
		LogFile.WriteLine("ANPR Whitelist copy tool")
		LogFile.WriteLine("This session was run on: " & Date & " at " & Time)
		LogFile.WriteLine("=========================================")
		LogFile.WriteBlankLines(1)
		WScript.Echo "finished opening log file"
	End Sub
	
	Public Sub OtherSub(x)
		WScript.Echo "entered othersub"
		'Open DestinationList.txt containing computer names
		'Keep DestinationList.txt in the same directory as the script file
		Set DestinationFile = FSO.OpenTextFile("DestinationList.txt", 1, False)
		
		'Read list file one line at a time and perform tasks
		Do While DestinationFile.AtEndOfStream <> True
			
			'*****************************************************************************
			'File1
			'*****************************************************************************
			
			strLine = DestinationFile.ReadLine
			
			strSourceFileName1 = "Whitelist.txt"
			strSourceFullPath1 = "D:\Temp\whitelist\Source\" & strSourceFileName1
			
			strTempDestFileName1 = "whitelisttemp.txt"
			strTempDestFullPath1 = strLine & strTempDestFileName1
			
			strFinaltempdest1 = strLine & strSourceFileName1
			
			LogFile.WriteLine(Date & " " & Time & " - Starting work on " & strLine & " - " & strSourceFileName1)
			
			If  FSO.FolderExists(strLine) Then
				LogFile.WriteLine(Date & " " & Time & " - Starting file copy to " & strTempDestFullPath1)
				FSO.CopyFile strSourceFullPath1, strTempDestFullPath1, True
				LogFile.WriteLine(Date & " " & Time & " - File copy completed")
				Set source1 = FSO.GetFile(strSourceFullPath1)		
				Set tempdest1 = FSO.GetFile(strTempDestFullPath1)
				
				If source1.Size = tempdest1.Size Then
					LogFile.WriteLine(Date & " " & Time & " - Source file size = " & Round(source1.Size/1000,2) & "kB - Temporary destination file size = " & Round(tempdest1.Size/1000,2) & "kB")
					If FSO.FileExists(strFinaltempdest1) Then
						FSO.DeleteFile strFinaltempdest1
						LogFile.WriteLine(Date & " " & Time & " - " & strFinaltempdest1 & " has been deleted")
					Else
						LogFile.WriteLine(Date & " " & Time & " - " & strFinaltempdest1 & " did not exist - skipping")
					End If
					
					tempdest1.name = strSourceFileName1
					LogFile.WriteLine(Date & " " & Time & " - " & strTempDestFileName1 & " has been renamed to " & strSourceFileName1)
				End If
			Else
				
				LogFile.WriteLine("Destination does not exist, skipping")
			End If
			
			LogFile.WriteBlankLines(1)
			
			'*****************************************************************************
			'File2
			'*****************************************************************************
			
			strSourceFileName2 = "Greylist.txt"
			strSourceFullPath2 = "D:\Temp\whitelist\Source\" & strSourceFileName2
			
			strTempDestFileName2 = "greylisttemp.txt"
			strTempDestFullPath2 = strLine & strTempDestFileName2
			
			strFinaltempdest2 = strLine & strSourceFileName2
			
			LogFile.WriteLine(Date & " " & Time & " - Starting work on " & strLine & " - " & strSourceFileName2)
			
			If  FSO.FolderExists(strLine) Then
				LogFile.WriteLine(Date & " " & Time & " - Starting file copy to " & strTempDestFullPath2)
				FSO.CopyFile strSourceFullPath2, strTempDestFullPath2, True
				LogFile.WriteLine(Date & " " & Time & " - File copy completed")
				Set source2 = FSO.GetFile(strSourceFullPath2)		
				Set tempdest2 = FSO.GetFile(strTempDestFullPath2)
				
				If source2.Size = tempdest2.Size Then
					LogFile.WriteLine(Date & " " & Time & " - Source file size = " & Round(source2.Size/1000,2) & "kB - Temporary destination file size = " & Round(tempdest2.Size/1000,2) & "kB")
					If FSO.FileExists(strFinaltempdest2) Then
						FSO.DeleteFile strFinaltempdest2
						LogFile.WriteLine(Date & " " & Time & " - " & strFinaltempdest2 & " has been deleted")
					Else
						LogFile.WriteLine(Date & " " & Time & " - " & strFinaltempdest2 & " did not exist - skipping")
					End If
					
					tempdest2.name = strSourceFileName2
					LogFile.WriteLine(Date & " " & Time & " - " & strTempDestFileName2 & " has been renamed to " & strSourceFileName2)
				End If
			Else
				
				LogFile.WriteLine("Destination does not exist, skipping")
			End If
			
		Loop
		
		LogFile.WriteBlankLines(1)
	End Sub 
End Class

When I run this, my output shows as

Initialise
terminating
new class called
finished mainsub

Can someone explain why the class is terminating early?

Many thanks
 
Soldato
OP
Joined
18 Oct 2002
Posts
7,492
Location
Maidenhead
Ahh, option explicit - I wasn't declaring 2 variables.

New issue

Initialise
new class called
Entered main sub
entered othersub
D:\Temp\whitelist\listcopy.vbs(99, 4) Microsoft VBScript runtime error: Object r
equired: 'LogFile'

terminating
D:\Temp\whitelist\listcopy.vbs(40, 3) Microsoft VBScript runtime error: Object r
equired: 'LogFile'

How do I make LogFile accessible in the OtherSub subroutine?
 
Back
Top Bottom