Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual Basic Error Handling

Posted on 2012-03-09
4
Medium Priority
?
538 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 59

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
HainKurt earned 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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