KorkOoO
KorkOoO

Reputation: 612

How to start a system "beep", from the built-in pc speaker, using a batch file?

I have written an interactive batch script where tasks sometimes take a long time to complete. After each task, the script prompts the user to decide whether to proceed to the next task, return to the main menu, or something else.

I would like to add an "Interactive Alarm" command that makes a small, short beep (like the one heard when turning on a PC) to alert the user of new questions.

Requirements:

Notes:

Is this possible, and how can it be implemented?

Upvotes: 37

Views: 82789

Answers (9)

npocmaka
npocmaka

Reputation: 57262

WARNING: rundll32.exe Kernel32.dll,Beep 750,300 no longer works well from the command line on modern windows systems as rundll32 no longer accepts integer values (again, through the command line) and this will play the beep with the default values which is too long (and frequency is irritating):

REM Again, with warnings about running this from the command line...
rundll32.exe Kernel32.dll,Beep 750,300

or

rundll32.exe cmdext.dll,MessageBeepStub

or

rundll32 user32.dll,MessageBeep

With rundll functions you won't need special symbols like ^G. With the first method you can also set the frequency and the time you want to beep, though see the warning that those parameters no longer work on modern systems from the command line and will instead play the annoying defaults.


UPDATE

Other options are calling Console.Beep from .NET using PowerShell:

powershell "[console]::beep(500,300)"

or using systemSounds.bat

call systemsounds.bat beep

The capability of beeping depends on the mainboard and if the mainboard has a system speaker - which has increasingly become a rarity as systems tend to depend solely on "normal" speakers instead. An alternative is to play sound through those speakers. Here are some options:

Using the speaking capabilities of the SAPI.SpVoice:

mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('beep')))"

Here this is wrapped in a batch file and the words can be passed as an argument.

SAPI.SpVoice can be used for playing WAV files and you have some packaged with the default Windows installation. You can use this script:

spplayer.bat  "C:\Windows\Media\Windows Navigation Start.wav"

Another option: Using the windows media player active-x objects to play a sound. On Windows XP it was not installed by default, but I think for the newer Windows versions it is. It also can play MP3 files:

call mediarunner.bat "C:\Windows\Media\Ring03.wav"

And one that is a little bit obscure - using the <bgsound> tag from internet explorer (which also can play mp3 files). Here's the script:

call soundplayer.bat "C:\Windows\Media\tada.wav"

And here's a way to use the BEL character to produce sound with easy to copy-paste code (I've called it a beeper.bat):

@echo off
setlocal
::Define a Linefeed variable
(set LF=^
%=-=%
)
  
for /f eol^=^%LF%%LF%^ delims^= %%A in (
   'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x07"'
) do echo(%%A

Upvotes: 83

opcode
opcode

Reputation: 469

Call Console.Beep from .NET using PowerShell:

powershell.exe [console]::beep(500,600)

So programmatically in Node.js it would look like this (Python or C would be similar):

require("child_process").exec("powershell.exe [console]::beep(500,600)");

Upvotes: 4

Qwerty
Qwerty

Reputation: 193

A bit late to the party, but I find this variation on @npocmaka version works for me with Windows 10:

REM This captures the Bell as a variable.
for /f %%g in ('%__APPDIR__%forfiles.exe /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "bel=%%g"
REM This produces the Bell sound.
set /P "=%bel%"<NUL

Upvotes: 0

tinku007
tinku007

Reputation: 54

This works for me..

enter image description here

there was a special character in line 4 which stackoverflow was omitting, code's pasted here: hashb.in/long

and line 5 and 6 can be used interchangeably of course.

Upvotes: -1

T3RR0R
T3RR0R

Reputation: 2951

The following can be used to issue a beep

  • without pausing the script
  • without creating a new line.
  • without requiring the use of a non-printable character
 Echo/| CHOICE /N 2> nul & rem BEL

It is a deliberate misuse of the choice command, that Echo's nothing via a pipe to Choice, causing a non-breaking error. STDERR is redirected to nul, and the default choice prompt is suppressed via the /N switch, meaning no new line is output.

If for some reason you wanted to reuse this annoying tone throughout a script, you could define it as a macro

Set "BEL=Echo/| CHOICE /N 2> nul"
%BEL%

Upvotes: 19

Jos&#233; L. Bello
Jos&#233; L. Bello

Reputation: 11

I think the better solution is echoing a ^G to a file from the cmd prompt and then type that file from within the script, that way you don't need to include control characteres in the batch file itself:

C:\> echo ^G>beep.snd

Now there's an ASCII 007 char in the "beep.snd" file, then from your .bat file all you have to do is type it or copy to the screen:

type beep.snd

or

copy beep.snd con > nul

Upvotes: 1

cascading-style
cascading-style

Reputation: 628

@echo off
echo BEEP.BAT by CSS---
echo PRESS ANY KEY TO HEAR A BEEP...
PAUSE>NUL
ECHO 
echo I BEEPED
PAUSE

there is an ASCII control code ^G after the echo. Just copy this code, and save it as ASCII/ANSI using a text editor.

Upvotes: 3

Stephan
Stephan

Reputation: 56180

It's not possible to type the BEL directly in (for example) notepad.

To get it, type echo ^G>>yourbatch.bat on the command line (don't type ^ G, but <Control>-G, which will be shown as ^G on the screen). That puts a strange looking character to the end of your file. That's the BELcharacter 0x007 ("control-G"). Just copy/move it to any echo command, you like. Also

set /p "input=^Ggive value: "

is possible (where the ^G represents that strange char)

Upvotes: 22

RGuggisberg
RGuggisberg

Reputation: 4750

use ECHO command to echo a CTRL G

Upvotes: 1

Related Questions