How can i schedule a vbscript to run cmd file?

I need to use task scheduler in 2008 server to run a vbscript that will do some check and run a cmd file if the check are true but for some reason the script doesnt run the cmd file.

here is the script.
Dim fso, oShell, objFiles
Set fso = CreateObject("Scripting.FileSystemObject")

Set oShell = WScript.CreateObject("WScript.Shell")
Set objFiles = fso.getfolder("S:\bkp_file").Files

If objFiles.count <> 0 Then
    oShell.Run "SQLRun.cmd",1,True
end if

Set fso = Nothing
Set oShell = Nothing
Set objFiles = nothing

now if i run the script manaully by click it then the cmd file runs. any help would be appreciated.
metroexpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daz_1234Commented:
Merry Christmas!

The first thought I have is, where is the 'SQLRun.cmd' file?

Depending on the current working directory at the time the script runs, the vbscript may not be able to find the batch file.

If the vbscript is in the same location as the batch file, the amended version of your script below should find it.  I also change the WScript.CreateObject to just CreateObject since that is not required here.

Dim fso, oShell, objFiles, strCurrDir
Set fso = CreateObject("Scripting.FileSystemObject")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set oShell = CreateObject("WScript.Shell")
Set objFiles = fso.getfolder("S:\bkp_file").Files

If objFiles.count <> 0 Then
    oShell.Run """" & strCurrDir & "SQLRun.cmd""",1,True
    ' or try the below:
    'oShell.Run "cmd.exe /c """ & strCurrDir & "SQLRun.cmd""",1,True
end if

Set fso = Nothing
Set oShell = Nothing
Set objFiles = nothing

Open in new window


Hope this helps,
Daz.
0
metroexpertAuthor Commented:
The script has no problem finding the cmd file in the same directory because if i run the script manaully by clicking it then everything works fine but i will not working when it is executed by the task scheduler.

here is the revised script:

Dim fso, oShell, objFiles, Mail
Set fso = CreateObject("Scripting.FileSystemObject")
Set Mail = CreateObject("CDO.Message")
Set oShell = CreateObject("WScript.Shell")

Mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"
Mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Mail.Configuration.Fields.Update

Mail.To = "Me@email.com"
Mail.From = "SQLAlerts@email.com"

Set objFiles = fso.getfolder("S:\bkp_file").Files

If objFiles.count <> 0 Then
    Mail.Subject = "SQL File Exist"
    Mail.TextBody = "There is a file in S:\BKP_File"
    Mail.Send
    oShell.Run "c:\SQL\SQLRun.cmd",1,True
end if

Set Mail = Nothing
Set fso = Nothing
Set oShell = Nothing
Set objFiles = nothing

I need to know why when task scheduler executes the script everything runs except for the oShell.Run command. I receive the email but the cmd file does not run.
0
Daz_1234Commented:
Give this a try:

    oShell.Run "cmd.exe /c c:\SQL\SQLRun.cmd",1,True

Daz.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

metroexpertAuthor Commented:
Tried that and it didn't work.
0
metroexpertAuthor Commented:
I got it, there is an error in the cmd file it self. I was getting an error code 1. thank you for your suggestions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metroexpertAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.