Make vbs wait until a time and then run a batch script - vbscript

I want to make a vbscript that waits until its 11:00AM and when it turns that time i want it to launch a batch script. I only know how to use
WScript.Sleep
But i want to make it run at a certain time.

Check system time with Now():
Dim time, WshShell
Set WshShell = CreateObject("WScript.Shell")
time = Now()
hm = Minute(time) + 100 * Hour(time)
' Only execute between 11:00AM and 11:30AM
While hm < 1100 Or hm > 1130
WScript.Sleep 2
time = Now()
hm = Minute(time) + 100 * Hour(time)
Wend
' Do your job here
WshShell.Run "some.bat", , False

Related

Set timer in VBS [duplicate]

This question already has answers here:
Close program opened by vbs script with same script
(2 answers)
Closed 11 months ago.
I want to modify the script to launch the calculator app for 2 minutes then close it. The script can launch the app only. Please help.
Set colMonitoredEvents = GetObject("winmgmts:")._
ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
Set strLatestEvent = colMonitoredEvents.NextEvent
If strLatestEvent.EventType = 4 Then
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
If objItem.name = "Calculator.exe" then objItem.terminate
Next
ElseIf strLatestEvent.EventType = 7 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc.exe", 1, false
End If
Loop
I want to modify it to launch and close the calculator.exe.
This is accomplished simply with Sleep and TaskKill. Note that, starting probably with Windows 8, the calculator is a UWP app and Calc.exe is used to launch it, but Calculator.exe is the process name to kill it. On Windows 7, it would be Calc.exe for both.
Set oWSH = CreateObject("Wscript.Shell")
oWSH.Run "Calc.exe", 1, False
WScript.Sleep 120000 'two minutes
oWSH.Run "TaskKill /im Calculator.exe /f", 0, False
If you want to retain the capability of the original script, which kills calculator when the computer goes to sleep and restarts it on wake, and add a two minute delay before the kill, that would be done like this:
Set oWSH = CreateObject("Wscript.Shell")
Set colMonitoredEvents = GetObject("winmgmts:").ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
Set strLatestEvent = colMonitoredEvents.NextEvent
If strLatestEvent.EventType = 4 Then
WScript.Sleep 120000 'two minutes
oWSH.Run "TaskKill /im Calculator.exe /f", 0, False
ElseIf strLatestEvent.EventType = 7 Then
oWSH.Run "calc.exe", 1, False
End If
WScript.Sleep 500
Loop
Note that I added a .5 second delay in the loop to prevent unnecessary CPU load.

Silently run EXE with VBScript

I have created a script which checks for the idle time using a program MousePos.exe and pushes the response back to itself to work out whether there has been any mouse movement. This all works fine, however everytime the MousePos.exe runs it flashes on the screen and I want to hide this. I have no idea how to do this, I expected it would be quite simple.
I was advised I need to Use the WshShell object's Run method instead of the WshScriptExec object but I have no idea how to do this and still read in the response from the EXE.
Function execStdOut(cmd)
Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
Dim aRet: Set aRet = goWSH.Exec(cmd)
execStdOut = aRet.StdOut.ReadAll()
End Function
'wait 2 seconds for things to calm now
WScript.Sleep 2000
'get initial Mouse XY
MPOS = execStdOut("cmd /c C:\Windows\MousePos.exe")
WScript.Sleep 10000 '10 seconds
'set initial idle counter
Idle = 0
Do While forever = 0
OLD = MPOS
MPOS = execStdOut("cmd /c C:\Windows\MousePos.exe")
If OLD = MPOS Then
Idle = Idle + 1
If idle = 12 Then '12 x 10 seconds for 2 minute timeout
idle = 0
Set objShell = WScript.CreateObject("WScript.Shell")
objshell.Run "C:\Windows\IERestart_countdown.hta", 1, True
End If
Else
idle = 0
End If
WScript.Sleep 10000 ' 10 seconds
Loop

VBScript not working when PC is locked

I'm running a VBScript that communicates to an exe file in Windows 7.
The VBScript works great!
The issues I have, is that once the PC has been in locked, goes to sleep or hiberation the VBScript doesn't communicate with the exe application.
The VBScript is running (I have a log that tells me every time a loop is complete, but its not communicating to the exe.
Below is code that is not working when the PC is locked.
Set WSHShell = WScript.CreateObject("WScript.Shell")
' info for exporting data
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine, cycles
Dim I
I = 0
Dim n
n = .1 'how often the program saves the data (in minutes)
cycles = 2 'how many times it will save
FileName = "C:\Users\Desktop\new.txt" 'location where the log file will save
Dim sl
sl = n * 60000 'change from seconds to ms for the sleep function
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output
Set MyFile = fso.OpenTextFile(FileName, ForAppending, True, TristateTrue)
' Write to the file.
MyFile.WriteLine "Log file for recording data from Yokogawa MX100 (" & cycles
& " cycles)"
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
Do while I < cycles
a = Now()
WScript.Sleep 1000
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.SendKeys "%A"
WScript.Sleep 1000
WSHShell.SendKeys "{DOWN}"
WScript.Sleep 1000
WSHShell.SendKeys "{ENTER}"
I = I + 1
MyFile.Writeline I & " of " & cycles & " at " & a & " --time of each cycle is
" & n & " minutes"
WScript.Sleep sl 'when sl is used loop time is in minutes
Loop
MyFile.Close
MsgBox ("Script has completed")
As Ansgar already said, it's pretty obvious that nothing will work while the PC sleeps or hibernates. In the case where the PC is locked, techniques that rely on window management or direct input such as SendKeys won't work as expected, because the user's session, along with user-level applications, is essentially shelved to make way for the login screen or another user.
You might want to do some research into the SendMessage/PostMessage API, or you can stop using VBScript and replace it with a Scheduled Task or system service that runs using a local service account, assuming you just want to execute an exe without any UI interaction.
The question is: do you need to go under hibernation or pc suspension? If not, you should simply configure or turn off those settings. By only locking the session, the vbscript should be run and generate an outpout without any trouble. By checking and configuring advanced windows energy settings it might help you solving this.

VBScript takes significantly longer when run using Windows Scheduler

I have a VBScript (.vbs) file which runs a SAS project. The program executes successfully when I run it manually by double-clicking the VBScript file and it takes about a minute and a half to finish.
However, when I schedule the VBScript file in the Windows Task Scheduler, it takes ridiculously long to run - the last time I tried I let it go for two hours before manually ending the task. I know the task is starting because when I refresh the task in Task Scheduler it says it is currently running.
I have several other VBScripts that I use to schedule other SAS projects, none of which have given me this issue. What gives?
Use this alias to force your script to execute with CSCRIPT interpreter. To debug timming, you can use the Timer() function, here is a example:
Set oWSH = CreateObject("WScript.Shell")
Call ForceConsole()
tmpTimer = Timer()
For i = 0 to 10000
a = a + 1
b = Int(Rnd * 500)
If b mod 2 = 0 Then b = 9
WScript.StdOut.WriteLine a
WScript.StdOut.WriteLine b
Next
WScript.StdOut.WriteLine "This loop took " & (Timer()-tmpTimer) & " miliseconds"
WScript.StdIn.ReadLine
Function ForceConsole()
If InStr(LCase(WScript.FullName), "cscript.exe") = 0 Then
oWSH.Run "cscript " & Chr(34) & WScript.ScriptFullName & Chr(34)
WScript.Quit
End If
End Function

Set interval in vbscript

I want to set interval time in vbscript
Following is my code
Set objWord = CreateObject("Word.Application")
endTime= Timer()
'Set visibilty for the client application
objWord.Visible = True
WScript.Sleep 1000
'Close MS word process
objWord.Quit
I found WScript.Sleep 1000 or WScript.Sleep(1000) 1000 is in millisecond
but both of them are not working fine, I am using window7
Quick-and-dirty batch command is the safest and surest (though not the most accurate) way to get a delay.
' Get a 10 seconds delay
Delay 10
Sub Delay( seconds )
Dim wshShell
Set wshShell = CreateObject( "WScript.Shell" )
wshShell.Run "ping -n " & ( seconds + 1 ) & " 127.0.0.1", 0, True
Set wshShell = Nothing
End Sub
As seen on Rob van der Woude's Scripting Page .Here you can also find lot of good scripts

Resources