Root Loop
Root Loop

Reputation: 3162

Batch file calling vbs wont run from Task scheduler

I have a batch file scheduled in task scheduler. If I run this batch file manually from CMD, It will do all the work well and taking about 20 mins. But if I schedule it in task scheduler, it finishes immediately when starting to run and shows the result is "successfully finished...." (VBS didnt run...) This batch file will call up 2 vbs scripts to run. I am not sure if there is something wrong with task scheduler or just batch file gets wrong code in there.

 @echo off

set logfile=c:\temp\Shop_Floor_Schedule.%TIME:~0,2%.log

echo %date% %time% > %logfile%

cscript  "c:\work\scripts\Export.vbs" >> %logfile% 2>&1

cscript  "c:\work\scripts\Schedule.vbs" >> %logfile% 2>&1

echo "batch complete" >> %logfile%

I got error massages in log file

Fri 06/19/2015 10:00:00.13 
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

starting Excel
opening workbook
c:\work\scripts\Export.vbs(30, 1) Microsoft Excel: Microsoft Excel cannot access the file 'C:\ntfs3\scripts\PRODUCTION.xls'. There are several possible reasons:

 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently open workbook.

I only get this message from task scheduler, but if run it from cmd manually, everything just well.

Upvotes: 2

Views: 1106

Answers (1)

Steve Kline
Steve Kline

Reputation: 805

Get out of the habit of logging using wscript.echo & stdOut vbscript outputs... its a terrible practice and reduces your ability to include logic to work around problems like this.

I would recommend that fix for method. Instead of using >> %log% 2>&1... Use a argument to your vbscripts for the logfile. It's a simple solution. Just point your scheduled task at this vbscript.. it should fix your issues for this batch file issue.

const forreading = 1, forwriting = 2, forappending, 8
set fso = CreateObject("Scripting.FileSystemObject")
set shell = WScript.CreateObject ("WScript.shell")
logfile = "C:\temp\Shop_Floor_Schedule" & Hour(now()) & ".Log"

' Start Export.Vbs
'============================================================    
Call WriteLog("Starting Export VBScript")
Shell.run "cmd /c cscript  c:\work\scripts\Export.vbs >> """ & logfile & """ 2>&1"


' Start Schedule.vbs
'========================================
Call WriteLog("Starting Schedule VBScript")
Shell.run "cmd /c cscript  c:\work\scripts\Schedule.vbs >> """ & logfile & """ 2>&1"

'Terminate Objects and exit script
'============================================================
Call WriteLog("Terminating Script")
Set objShell = Nothing
Set fso = nothing

Sub WriteLog(strMessage)
    set logf = fso.OpenTextFile(logfile, forappending, true)
    strTime = Hour(Now()) & ":" & Minute(Now())
    logf.writeline = Date() & " - " & strTime & "   " & strMessage
    logf.close
    set logf = nothing
end sub

Upvotes: 0

Related Questions