Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append Date of Success or Erros to Text File 1

Status
Not open for further replies.

Brycspain

IS-IT--Management
Joined
Mar 9, 2006
Messages
150
Location
US
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

 
Replace this:
FSO.CreateTextFile("results.txt"), False
Set File=FSO.GetFile("c:\results.txt")
Set objFile = objFSO.OpenTextFile("C:\results.txt", ForAppending)
with this:
Set objFile = FSO.OpenTextFile("C:\results.txt", ForAppending, True)

Tip: use the Option Explicit instruction ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, it worked great.

One more question: How do I combine all 3 statements on one line? The output I get now takes 3 lines and looks like this:

6/15/2006Unmapping worked!
6/15/2006Mapping Worked!
6/15/2006Copy Worked!

Of course I will remove the date from the second and third entries.

 
Have a look at the difference between the WriteLine and Write methods.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for making me look it up PHV...I usually research before I ask a question however I just got a bit exited.
 
PHV,

I changed the code to read: (I took the lines out of the script)

Code:
objFile.Write Now() & "Unmapping worked!" 
objFile.Write Now() & "Mapping Worked!"
objFile.Write Now() & "Copy Worked!"

It writes to the text file:

date time Unmapping worked! Mapping Worked! Copy Worked!

However, the next time the script runs it appends to the same line instead of starting a new one.

How can a put a "carriage return" into the script?
 
Perhaps this ?
'close the txt file
objFile.WriteLine ""
objFile.Close

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I learned something new today. Thanks so much for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top