Verbal Alerts in Batch and other Windows Scripts

Lee W, MVPTechnology and Business Process Advisor
Jack of All Trades with an interest in facilitating networking through social interaction of IT Professionals
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly instantly when you only have a little to do and could take many minutes when you have a lot to do and short of paying close attention to the progress of the script (about as interesting as reading pi), you probably won't know when the script actually completes.  
To solve this problem, at least for myself, I wrote the following VBS script that verbally tells you when it's done.  This script, when saved to a .vbs file, should execute much like any other command line command.  Thus, it can be easily integrated into any batch script (really any scripting language that can execute a command line command) and with a little tweaking, incorporated into other VBS scripts.
Option Explicit
                      On Error Resume Next
                      Dim Speaker 'Becomes the object that is used to speak.
                      Dim VoiceList, Voice 'Used in the loop to list the available voices
                      Dim Words 'Words to speak
                      Dim Speed 'Speed to speak, range is -10 (slow) to 10 (fast)
                      'Create the speaker object
                      Set Speaker = CreateObject("SAPI.SpVoice")
                      'Get the words to speak
                      Words = WScript.Arguments.Item(0)
                      If Err.Number = 9 Then 'No command line parameters entered, including words to speak.  Popup a usage dialog
                       Words="Nothing to say!" & vbCrLf & vbcrlf & "Syntax:" & vbCrLf & "SPEAK ""hello world"" [slow|normal|fast|-10 to 10]"
                       MsgBox Words
                       If Lcase(WScript.Arguments.Item(0)) = "list" Then 'list the voices available - most systems only have the default Microsoft Ana.  
                        For each Voice in Speaker.GetVoices
                         VoiceList = VoiceList & Voice.GetDescription & vbcrlf
                        MsgBox VoiceList
                       Else 'speak the word or words in quotes specified as the first parameter
                        Select Case LCASE(WScript.Arguments.Item(1)) 'If a second parameter is specified, it's the speed of the voice.  Talk slow or Talk fast.
                         Case "slow"
                          Speed = -5 'slower
                         Case "fast"
                          speed = 5 'faster
                         case else 'either set the speech speed to normal OR use an absolute value (-10 to 10 - very slow to very fast)
                          If IsNumeric(LCASE(WScript.Arguments.Item(1))) = True Then 'If the second parameter is a number then
                           If CInt(WScript.Arguments.Item(1)) > -11 And CInt(LCASE(WScript.Arguments.Item(1))) < 11 Then 'checking that the number is between -10 and 10
                            Speed = WScript.Arguments.Item(1)
                           Else 'an invalid number was entered, using the standard speed.
                            Speed = 0
                           End If
                          Else 'speed not specified, defaulting to normal
                           Speed = 0
                          End If
                        End Select
                        Speaker.Rate = speed 'set the speech speed for the speaker
                        Speaker.Speak Words 'say the words as obtained earlier. 
                       End If
                      End If

Open in new window

Save the above code as "speak.vbs" somewhere that's in your path environment variable.
To test, create a small batch file that does nothing more than provide a directory listing:
@echo off
                      dir c: /s /b
                      speak "Directory listing complete"

Open in new window

And then execute the batch file.
(Just make sure your sound is not muted and your speakers are on!)

I create two folders on every system I setup - C:\Windows\Utils and C:\Windows\Scripts.  I then add these folders to my system's path statement.  This allows me to store all my scripts and utilities in appropriate places, separate from Windows files, and access them easily regardless of where my script needs to execute.  Otherwise, you would constantly need to reference the full path of the scripts you create or store them in places like C:\Windows\System32 - a less than ideal place.

If you prefer to use vbs scripts yourself the "magic line" (not specifically in the above script) with no alterations is:
CreateObject("SAPI.SpVoice").Speak "Hello World"

Open in new window

And if you're familiar with using objects in VBS scripts, you'll likely easily decode my script and be able to a adapt it to whatever project you like.

It's also worth noting that there are many variants of VB and the syntax will likely be the same or similar if you want to incorporate a verbal alert in a macro for an Office file like an Excel spreadsheet or Word document - VBA and VBS are very similar.

One final note - if the words you're using aren't pronounced properly, consider spelling phonetically.
Lee W, MVPTechnology and Business Process Advisor
Jack of All Trades with an interest in facilitating networking through social interaction of IT Professionals

Comments (1)

Steve KnightIT Consultancy

Nice... I'd used different ways of doing this in the past and hadn't seen this article before.  Nice easy way and going to get used to annoy people in offices to remind them to do stuff they have forgotten!

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.