vhadalgi
vhadalgi

Reputation: 7189

How to redirect error to a Text file in VBS

How to redirect Output to a Text file using vb script

I have a code like this

Dim tsIn
set tsIn = goFS.OpenTextFile("E:\..t.csv") 'if file doesn't exist then error

 If Err.Number <> 0 Then
    MsgBox Err.Description & "Input File Did not open" '> "E:\t.txt"
    Exit Sub
 End If

But how to redirect the error i get if a file doesn't exist in the folder?

i tried > but there's no result!

Upvotes: 0

Views: 6477

Answers (2)

js2010
js2010

Reputation: 27491

Redirect error to output. %errorlevel% will still be 0 though.

cscript myscript.vbs > myscript.log 2>&1

Upvotes: 1

Pankaj Jaju
Pankaj Jaju

Reputation: 5471

Try something like the following

On Error Resume Next

Set objShell = CreateObject("Wscript.Shell")
errTxtFile = objShell.SpecialFolders("Desktop") & "\21777595.txt"
Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set FSO_Handle = objFSO.OpenTextFile (errTxtFile,8,True)

Err.Raise 6 'To test if the error is getting stored or not

 If Err.Number <> 0 Then
    FSO_Handle.WriteLine Now & " - Error : " & Err.Number & ": " & Err.Description
 End If

Upvotes: 1

Related Questions