I wrote a script that maps a drive and copies a folder to another server. What I would like to do is log the date of success or error on each segment of the script and have it append to a text file each time the script runs (Once per week). The script creates the text file however it doesn't append any information to it. Could someone please point out the error of my ways? Thanks
Code:
On Error Resume Next
Dim FSO, WshNetwork, objFile
Const forWriting = 2
Const ForAppending = 8
Set FSO = CreateObject("Scripting.FilesystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
FSO.CreateTextFile("results.txt"), False
Set File=FSO.GetFile("c:\results.txt")
Set objFile = objFSO.OpenTextFile("C:\results.txt", ForAppending)
'Disconnect t: drive if mapped
WshNetwork.RemoveNetworkDrive "T:", True, True
If Err.Number <> 0 Then
objFile.WriteLine Date() & "Unmapping Failed!" & Err.Number & "Error Description: " & Err.Description
Err.Clear
Else
objFile.WriteLine Date() & "Unmapping worked!"
End If
Err.Clear
'pause the script to let the drive unmap
wscript.sleep 300
'map T: drive
WshNetwork.MapNetworkDrive "T:", "\\server\subfolder\subfolder\subfolder\subfolder", True, "domain\username", _
"password"
If Err.Number <> 0 Then
objFile.WriteLine Date() & "Mapping Failed! " & Err.Number & "Error Description: " & Err.Description
Err.Clear
Else
objFile.WriteLine Date() & "Mapping Worked!"
End If
Err.Clear
'pause the script to let the drive map
wscript.sleep 300
If FSO.FolderExists("c:\mainfolder") Then
FSO.CopyFolder "c:\mainfolder", "t:\mainfolder", True
End If
If Err.Number <> 0 Then
objFile.WriteLine Date() & "Copy failed! " & Err.Number & " Error Description: " & Err.Description
Err.Clear
Else
objFile.WriteLine Date() & "Copy Worked!"
End If
'close the txt file
objFile.Close
' Clear memory
Set FSO = Nothing
Set WshNetwork = Nothing
Set objFile = Nothing
'quit script
wscript.quit