kid
kid

Reputation: 140

CMD in vbscript

I want to write a simple vb script to automate shutdown in windows.

the code I am using is :

Dim ti
ti=InputBox("enter time in minutes")
ti=ti*60
Set objShell=CreateObject("WScript.Shell")
objShell.Run "cmd shutdown /s /t "& ti & " "

but when I enter the time and press enter , all I get is an command prompt window and nothing happens

I even tried by setting a default value for time and specifing the complete path for shutdown.exe ,but nothing seems to be working

  Set WshShell = WScript.CreateObject("WScript.Shell")
  Command = "C:\Windows\System32 shutdown.exe -s -t 600 "
  WshShell.Run Command

can u please correct me and guide me towards the right code ....

Upvotes: 0

Views: 8965

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

If you want to run commands in cmd you have to use either /k (keep cmd window open after command finishes) or /c (close cmd window after command finishes). Here's the canonical way to do this:

ti = InputBox("enter time in minutes")
ti = ti * 60
CreateObject("WScript.Shell").Run "%COMSPEC% /c shutdown -s -t " & ti

%COMSPEC% is a system environment variable with the path to cmd.exe.

Upvotes: 1

Derek Tomes
Derek Tomes

Reputation: 4007

It looks like you're missing a backslash in your path:

  Set WshShell = WScript.CreateObject("WScript.Shell")
  Command = "C:\Windows\System32\shutdown.exe -s -t 600 "
  WshShell.Run Command

Upvotes: 3

Related Questions