Solved

Visual Basic Error Handling

Posted on 2012-03-09
4
535 Views
Last Modified: 2012-03-26
Trying to have error output to a log. It does not seem to be working. If there is an easier way of doing this that would be great. This is what I have:

'Constants
Const FOF_CREATEPROGRESSDLG = &H0&
path = "C:\usertest\"
'Declarations
Set objfso         = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objfso.OpenTextFile("UsermoveTestlist.Csv")
Set objShell = CreateObject("Shell.Application")


Do While Not objTextFile.AtEndOfStream

      'Add error checking and response
      On Error Resume Next
      arrStr       = Split(objTextFile.ReadLine,",")
      oldUser      = arrStr(0)
      newUser      = arrStr(1)
      TargetFolder = path & newUser
      srcFolder    = path & oldUser
      Set objFolder = objShell.NameSpace(TargetFolder)

      'If TargetFolder does not exist. Make it!

      If Not objFSO.FolderExists(srcFolder) or ObjFSO.FolderExists(srcFolder) Then
            WScript.Echo "Creating folder: " & TargetFolder
            objFSO.CreateFolder(TargetFolder)
                 
                       
      
        End If

      WScript.Echo "Attempting to move " & srcFolder & " to " & TargetFolder
      'objFolder.CopyHere TargetFolder, FOF_CREATEPROGRESSDLG
      objFSO.CopyFolder srcFolder , TargetFolder '

Public Sub WriteToErrorLog(ByVal Msg As String,   'getting an error here
       ByVal stkTrace As String, ByVal title As String)

   'check and make the directory if necessary; this is set to look in
   the application folder, you may wish to place the error log in
   another location depending upon the user's role and write access to
   different areas of the file system
    If Not System.IO.Directory.Exists(Application.StartupPath & 
    "\Errors\") Then
        System.IO.Directory.CreateDirectory(Application.StartupPath & 
        "\Errors\")
    End If

    'check the file
    Dim fs As FileStream = New FileStream(Application.StartupPath & 
    "\Errors\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
    Dim s As StreamWriter = New StreamWriter(fs)
    s.Close()
    fs.Close()

    'log it
    Dim fs1 As FileStream = New FileStream(Application.StartupPath & 
    "\Errors\errlog.txt", FileMode.Append, FileAccess.Write)
    Dim s1 As StreamWriter = New StreamWriter(fs1)
    s1.Write("Title: " & title & vbCrLf)
    s1.Write("Message: " & msg & vbCrLf)
    s1.Write("StackTrace: " & stkTrace & vbCrLf)
    s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
    s1.Write("================================================" & vbCrLf)
    s1.Close()
    fs1.Close()

End Sub

      Err.Clear
Loop

'Close File stream and clean up
objTextFile.Close

Set fso = Nothing
Set objFolder = Nothing
Set objTextFile = Nothing
Set objShell = Nothing
0
Comment
Question by:Darrell Kirby
  • 3
4 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 37703275
first of all move "Public Sub WriteToErrorLog(...)... end sub" out of loop :)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37703290
second where do you call this WriteToErrorLog?
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37703496
i guess you found a vb code and copied pasted into your vb script :)

here how you should do

'Constants
Const FOF_CREATEPROGRESSDLG = &H0&
Const path                  = "C:\temp\"

'Declarations
dim objfso,objTextFile,objShell
Set objfso            = CreateObject("Scripting.FileSystemObject")
Set objTextFile       = objfso.OpenTextFile("UsermoveTestlist.Csv")
Set objShell          = CreateObject("Shell.Application")

sub MoveUserFiles(oldUser, newUser) 
	TargetFolder    = path & newUser
	srcFolder       = path & oldUser
	Set objFolder   = objShell.NameSpace(TargetFolder) 
	
	msgbox "Copy " & oldUser & " --> " & newUser & " started..."
	'If TargetFolder does not exist. Make it!
	If Not ObjFSO.FolderExists(TargetFolder) Then
		WScript.Echo "Creating folder: " & TargetFolder 
		objFSO.CreateFolder(TargetFolder)
	End If

	WScript.Echo "Attempting to move " & srcFolder & " to " & TargetFolder
	objFSO.CopyFolder srcFolder , TargetFolder
	msgbox "Copy " & oldUser & " --> " & newUser & " completed..."
end sub

Do While Not objTextFile.AtEndOfStream
	'Add error checking and response
	On Error resume next
	arrStr          = Split(objTextFile.ReadLine,",")
	oldUser         = arrStr(0)
	newUser         = arrStr(1)
	
	MoveUserFiles oldUser, newUser

	If Err.Number <> 0 Then
		MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
		WriteToErrorLog Err.Number, Err.Description, oldUser & " --> " & newUser
		Err.Clear      ' Clear the error.
	End If
Loop

'Close File stream and clean up
objTextFile.Close

Set fso = Nothing
Set objFolder = Nothing
Set objTextFile = Nothing
Set objShell = Nothing

Public Sub WriteToErrorLog(ByVal Msg, ByVal stkTrace, ByVal title)
	Dim fso, tf
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set tf = fso.OpenTextFile("Errors\errlog.txt", 8, True) ' forAppending, make sure you have Errors folder in running folder
	tf.WriteLine "Title      : " & title
	tf.WriteLine "Message    : " & msg
	tf.WriteLine "StackTrace : " & stkTrace
	tf.WriteLine "Date/Time  : " & Now()
	tf.WriteLine "================================================"
	tf.Close
End Sub

Open in new window

0
 

Author Comment

by:Darrell Kirby
ID: 37703512
You are correct! I will try this. Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question