1

I am new to Windows Scripting. I have a simple script for archiving using WinRAR CLI utility. I have to schedule this script using batch file. During archiving there are some errors and I want them to write in a simple text file or at least I can write entire output of archiving in a file. How can I change my code to do this?

Dim MyDate
Dim OutputFile 
const WaitUntilFinished = true, DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0 

MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar" 

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\Users\ABC\Desktop\"
objShell.Run "C:\windows\Rar.exe a .\VBScripts\backups\" & OutputFile & " software", ShowWindow, WaitUntilFinished

objShell.Popup "Archiving Completed Successfully!",5, "Scheduled Backup"
Set objShell = Nothing

Batch file is like this;

@echo off 
start /wait C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 
Saqib Iqbal
  • 339
  • 5
  • 13

2 Answers2

2

Change your command line to include redirection to a log file:

logfile = "C:\path\to\your.log"
objShell.Run "%COMSPEC% /c C:\windows\Rar.exe a .\VBScripts\backups\" & _
  OutputFile & " software >""" & logfile & """", ShowWindow, WaitUntilFinished
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Use this function instead of WScript.Shell.Run:

' Runs an external program and pipes it's output to
' the StdOut and StdErr streams of the current script.
' Returns the exit code of the external program.
Function Run (ByVal cmd)
   Dim sh: Set sh = CreateObject("WScript.Shell")
   Dim wsx: Set wsx = Sh.Exec(cmd)
   If wsx.ProcessID = 0 And wsx.Status = 1 Then
      ' (The Win98 version of VBScript does not detect WshShell.Exec errors)
      Err.Raise vbObjectError,,"WshShell.Exec failed."
   End If
   Do
      Dim Status: Status = wsx.Status
      WScript.StdOut.Write wsx.StdOut.ReadAll()
      WScript.StdErr.Write wsx.StdErr.ReadAll()
      If Status <> 0 Then Exit Do
      WScript.Sleep 10
   Loop
   Run = wsx.ExitCode
End Function

Call script instead of start in your batch and use redirection:

script //nologo C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 2> errors.txt
torvin
  • 6,515
  • 1
  • 37
  • 52