Solved

Visual Basic Error Handling

Posted on 2012-03-09
4
530 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
Comment Utility
first of all move "Public Sub WriteToErrorLog(...)... end sub" out of loop :)
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
second where do you call this WriteToErrorLog?
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
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
Comment Utility
You are correct! I will try this. Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now