How to delay in VB Script? - visual-studio-2010

How can I delay a Vb Script?
The following codes did not work for me:
wscript.sleep 1000
Delay 10
Sub Delay( seconds )
Dim wshShell, strCmd
Set wshShell = CreateObject( "Wscript.Shell" )
strCmd = "%COMSPEC% /C (PING -n " & ( seconds + 1 ) & " 127.0.0.1 >NUL 2>&1 || PING -n " & seconds & " ::1 >NUL 2>&1)"
wshShell.Run strCmd, 0, 1
Set wshShell = Nothing
End Sub
dim oshell
set oshell = CreateObject("Wscript.Shell")
obshell.run "%windir%\system32\rundll32.exe kernel32.dll Sleep 5000"

The first statement, WScript.Sleep 1000, does work. Your error must be somewhere else.
Proof
Create a file test.vbs on your desktop:
WScript.Echo Time()
WScript.Sleep 2000
WScript.Echo Time()
Run as follows:
C:\Users\...\Desktop>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
11:31:34
11:31:36
Note the two second difference. (If this exact script produces a different outcome on your PC, please say so in the comments and accept my apology.)

You cannot use the WScript object in VB script custom actions. The WScript object is supplied by the script environment when you run it in Windows Script Host, and is not in the MSI environment. That means there is no way to do a delay, so maybe you could describe the problem you're having that the delay might solve.

Related

Is it possible to write a Windows 10 script opening speaker settings?

I want to write a windows 10 batch script to change my microphone's output volume, as Skype apparently is not able to provide me with consistent settings. Can you point me in the right direction on how to achieve this?
To change the volume You can build 2 vbs files :
soundown.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(174) & " 5}"
soundup.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(175) & " 20}"
and then call them from your .batfile :
#echo off
cscript.exe /nologo soundown.vbs
or the same with soundup.vbs
EDIT:
You can modify the steps it will modify the volume by modifing the value 5 and 20 in the VBSfiles

How to hit Enter in cmd from VBSCript

Trying to run few Commands from VBScript
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c cd C:\Script & lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
The Second command lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property requires me to hit enter to complete its execution. Any idea how to do that through VBScript.
Tried giving
objShell.Sleep(1000)
objShell.SendKeys("{ENTER}")
but nothing happens. Still waits for manual hit of enter!
The solution is:
WshShell.SendKeys "{ENTER}"

WshShell.AppActivate doesn't seem to work in simple vbs script

total vbs scripting newb here. I'm trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what my script currently looks like:
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "HostsMan"
WshShell.SendKeys "%{F4}"
The second line doesn't seem to work. I know line 3 works because it activates the Windows shutdown menu. Is there something I'm missing?
Update/more info: Manually entering alt-F4 does close it, so I know this should work. I also tested this script with other open windows and they close just fine. Additionally, HostsMan is opened with Admin privileges, so I tried running the script as a task set with highest privileges to see if that would do it, and still no go. But that does work with other open windows running with Admin privileges. Frustrating!
I've tried it, too, and couldn't get it to work. There must be something about the window class, perhaps, where AppActivate doesn't see it as a top-level window?
In any event, AppActivate also lets you pass the process ID instead of the window title. When I installed HostsMan, the process name was hm.exe, so I'll use that in my example below.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, "hm.exe", vbTextCompare) = 0 Then
' Activate the window using its process ID...
With CreateObject("WScript.Shell")
.AppActivate Process.ProcessId
.SendKeys "%{F4}"
End With
' We found our process. No more iteration required...
Exit For
End If
Next
Alternative solution using WMIService (no loop through all processes required):
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'")
If colItems.Count = 0 Then
WScript.Echo "Process not found"
Wscript.Quit
End If
For Each objProcess in colItems
WshShell.AppActivate(objProcess.ProcessId)
Exit For
Next
The key here is to put a small pause after the 'run' command but before the 'appactivate' command so the application shows up in the process list.
WshShell.Run "calc"
WScript.Sleep 100
WshShell.AppActivate "Calculator"
To solve the problem of AppActivate you have to use loop and if condition statement to check if the active windows is the target windows or not because sometime you deselect or the system deselect the target windows before execute the sendkeys directly so you got error.
I create this tight strict way
Set WshShell = CreateObject("WScript.Shell")
for i=0 to 300 ' this loop will continue about 30 sec if this not enough increase this number
Rtn=WshShell.AppActivate("HostsMan") ' HostMan have to be the windows title of application or its process ID
If Rtn = True Then
WshShell.SendKeys "%{F4}" ' send key to click ALT+F4 to close
wscript.sleep 100 ' stop execute next line until finish close app
Rtn=WshShell.AppActivate("HostsMan")
If Rtn=False Then ' using nested If to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
wscript.sleep 100
Next
Dim sh : Set sh =CreateObject("Wscript.Shell")
sh.Exec "calc.exe"
Wscript.Sleep 1000
sh.Exec "taskkill /f /FI ""WINDOWTITLE eq ""calc*"""
OR
sh.Run "taskkill /f /im calc.exe",0,False

VBScript saying "No Application is associated with the specified file for this operation"?

I'm writing a simple VBScript to write to a custom windows event log using eventcreate.
FOR I = 0 to 5
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next
However, whenever I try to run it through the command prompt, I get the following message:
C:\testlog.vbs(6, 5) (null): No application is associated with the specified file for this operation.
From what I can tell, I'm doing exactly what the online examples are telling me to do, I just can't seem to replicate it. What am I doing wrong?
I ran your script and it worked as expected on a win 7 laptop. Ensure you are running the script with admin rights. I changed the script a little, I moved the Set statement out for the For...Next loop. There is no need to continue to set the WshShell object on each loop, setting it once for the entire script is fine in this instance.
Dim WshShell, strCommand
Set WshShell = WScript.CreateObject("WScript.Shell")
For I = 0 to 5
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next

VB Script to open multiple programs at once

Right im looking for a script that I can click on after I have logged in to open various programs just to save me a bit of time. I have managed to get a script to open one but as a bit of a newbie can someone provide advice.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe "" ""-
express:dvla.servicecenter.fs.fujitsu.com.12680"""
Set objShell = Nothing
You might be overthinking it a bit to use VBScript or Powershell for this job. A batch file will work.
#echo off
start "c:\Program Files\Folder 1\program.exe"
start "c:\Program Files\Folder 2\program.exe" -switch -argument
exit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("Path to program")
wscript.sleep (100)
objShell.Run("Path to program")
wscript.sleep (100)
wscript.quit
I do not have scguiw32.exe, so I created simple script which opens file in notepad and in word.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "C:\Windows\notepad.exe c:\dump.txt"
objShell.Run """C:\Program Files (x86)\Microsoft Office\Office14\winword.exe"" c:\dump.txt"
Set objShell = Nothing
BTW Instead of vbscript you can use now powershell, and powershell script is much more easy to understand. For example above one will be: Create run.ps1 with content
& 'C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE' c:\dump.txt
notepad.exe C:\dump.txt
Click it right-click and choose Run with Powershell
Here is how to use vbscript to create an array of programs you want to run and then execute each one.
'---Declare Variables
Dim objShell, strprogram1, strProgram2, colprograms, item
'---Create Scripting Shell Object
Set objShell = CreateObject("WScript.Shell")
'---Create Program Variables
strProgram1 = """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe"" ""-express:dvla.servicecenter.fs.fujitsu.com.12680"""
strProgram2 = "C:\Windows\notepad.exe C:\Dump.txt"
'---Add Variables to an Array
colPrograms = Array(strProgram1,strProgram2)
'---Run each program in the array once
For Each item In colprograms
objShell.Run item
Next
WScript.Echo "Done."

Resources