-1

Basically I Shutdown my PC using shutdown timer in cmd. That is,

Shutdown -s -t xxx

(where xxx being the seconds I enter. Example, Shutdown -s -t 3600, which means in 1 hour my PC will shutdown automatically).

Now I want to write a VBS where I will enter Seconds in inputbox and those seconds will be put into run the command. But i am unable to run the command successfully.

Below is my code:

Option Explicit
Dim obj,a,x
Set obj=CreateObject("WScript.Shell")
a=InputBox("Enter time in Seconds")
obj.Run "shutdown -s -t"&a
Set obj=Nothing
user692942
  • 16,398
  • 7
  • 76
  • 175
Mohammed Sajjad
  • 35
  • 3
  • 10
  • Answer hasn't been accepted by clearly a duplicate of [Run Command Line & Command From VBS](http://stackoverflow.com/q/16087470/692942) – user692942 Sep 14 '15 at 16:11
  • How about the at least specify what error you received as @Yimin could have simply pointed out in the comments you have a missing space between `-t"&a` should be `-t " & a`. – user692942 Sep 14 '15 at 16:34

1 Answers1

0

Do you need a space between -t and the time?

obj.Run "shutdown -s -t " & a

And because you're destroying the Shell object, try adding this:

obj.Sleep a * 1000 + 1000

Just after obj.Run ..., but before Set obj = Nothingand see if keeping the Shell object alive allows the command to complete.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
  • Both dashes and forward slashes are acceptable. Give it a try. – user692942 Sep 14 '15 at 16:35
  • No Buddy. Its not working yet... – Mohammed Sajjad Sep 14 '15 at 20:15
  • @MohammedSajjad - Might be because you're destroying the object and exiting the script. I read up and it doesn't look like the operation is queued. Have updated answer. – Yimin Rong Sep 14 '15 at 20:27
  • 1
    @Lankymart - read up on `shutdown`, it's one of the reverse ones, dashes implemented before slashes! Have removed the suggestion. – Yimin Rong Sep 14 '15 at 20:28
  • 1
    @MohammedSajjad Before you can expect us to help you update your question and tell us what error you are getting. Saying it doesn't work tells us nothing be descriptive. Lets face it we can't read minds. – user692942 Sep 14 '15 at 22:21
  • Not being funny but if you had left your answer until the OP provided more information we wouldn't be here having to explain all this. The fact is the question needs more clarity before anyone should attempt answering it. Plus the linked question I feel will have all the information the OP needs anyway. – user692942 Sep 15 '15 at 08:20
  • Why would you need `obj.Sleep a * 1000 + 1000`? Could you not just set `bWaitOnReturn` argument to `True`? `obj.Run "shutdown -s -t " & a, , True` should block until the command has completed. – user692942 Sep 15 '15 at 08:57
  • @Lankymart - I like that better. Had forgotten about the wait until completed option. Anyway question is closed, the OP should have enough to figure this out. – Yimin Rong Sep 15 '15 at 13:26
  • Yimini bang answer solved my issue... Thanks everyone for the support.. – Mohammed Sajjad Sep 15 '15 at 17:20
  • @MohammedSajjad What part solved it? This isn't a support site just a thanks and goodbye. Could you explain what you did to fix it? Not just for your benefit but for everyone who comes here looking for the answer. – user692942 Sep 15 '15 at 18:37