I have written a batch script in an interactive mode, for making some tasks.
Sometimes, These tasks takes a long time to be finished, and then the batch asks if the
@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.
use ECHO command to echo a CTRL G
rundll32.exe Kernel32.dll,Beep 750,300
or
rundll32.exe cmdext.dll,MessageBeepStub
or
rundll32 user32.dll,MessageBeep
With rundll functions you''ll no need special symbols like ^G
. With the first method you can also set the frequency and the time you want to beep.
UPDATE
another options are:
powershell "[console]::beep(500,300)"
or using systemSounds.bat
call systemsounds.bat beep
rundll32.exe Kernel32.dll,Beep 750,300
- this does not work well on modern windows systems as rundll32 no longer accepts integer values through command line and this will play the beep with the default values which is too long (and frequency is irritating)
As the capability of beeping depends also on the mainboard and if the mainboard has a system speaker - which more and more becomes a rarity - the systems more and more depend on the normal speakers. So an option is to play sound through them. 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 coming with the 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 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
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 BEL
character 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)
The following can be used to issue a beep
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%
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