1

Whenever I get an email that meets a criteria I run a batch file that takes a piece of the information and compares it to one file, then writes that info to two files and plays a sound.

What I would like to do is, if 20 seconds have not passed since the last file update (new_follower.txt), then do not play the sound. (it can get annoying it played to repeatedly.)

This is my file.

@ECHO OFF
SET "str=%1%,"
SET str=%str: is now following you on Twitch=%
SET str=%str:"=%

find /c "%str%" "D:\~MY STUFF\Live Stream!\keep_listr.txt"
if %errorlevel% equ 1 goto notfound
goto done

:notfound

ECHO %str% >> "D:\~MY STUFF\Live Stream!\new_follower.txt"
ECHO %str% >> "D:\~MY STUFF\Live Stream!\keep_listr.txt"

"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" --play-and-exit "D:\~MY STUFF\Live Stream!\Donation Alerts\surf.wav"

echo %str%

goto done
:done

Also, in Windows 7 is there a way to play the sound without launching an application like I am currently doing?

Thanks in advance

  • 1
    There's no simple native batch way. You can convert `%time%` to seconds (ignoring midnight rollover) and keep track of the elapsed time. VBS script may have a method to play a sound file. – foxidrive Apr 21 '14 at 04:40
  • as for the player : http://superuser.com/questions/539646/what-replaced-sndrec32-from-windows-xp-in-windows-7# – npocmaka Apr 21 '14 at 07:23
  • http://wsh2.uw.hu/ch14g.html – npocmaka Apr 21 '14 at 07:49
  • echo ^g (note that is Ctrl + G not ^ then g). – tony bd Apr 21 '14 at 08:19
  • Batch/VBS to play an MP3/Wav sound : http://stackoverflow.com/questions/20418730/batch-file-to-play-a-song/20422720#20422720 – SachaDee Apr 21 '14 at 11:54
  • Making a beep To make a beep in MSDos mode use echo followed by Ctrl + G, at the dos command prompt echo ^G or in a batch file use Ctrl P Ctrl G (Edit.com uses Ctrl + P to tell it the next key is a control key). In Windows use Start C:\WINDOWS\MEDIA\NOTIFY.WAV or mplayer2 /play /close C:\WINDOWS\MEDIA\NOTIFY.WAV – tony bd Apr 21 '14 at 11:59

2 Answers2

1

As the target OS is windows 7, this code makes use of forfiles to retrieve file time in seconds. The rest of the code is from a "run time meter" i was using. It will handle the midnight rollover but will not test for time difference over multiple days. Adapt as needed. This just returns the age in cents of second of the current batch file and this value formated as a usual time string.

@echo off

    setlocal enableextensions disabledelayedexpansion

    call :getFileTime "%~f0" fileTime
    call :getElapsedTime "%fileTime%" "%time%" fileAge fileAgeFormatted
    echo %fileAge% %fileAgeFormatted%

    endlocal
    exit /b

:getFileTime file returnVariable
    for /f "tokens=*" %%a in ('forfiles /p "%~dp1." /m "%~nx1" /c "cmd /c echo @ftime"') do set "%~2=%%~a"
    exit /b

:getElapsedTime time1 time2 returnVariableCent returnVariableTimeFormat
    setlocal enableextensions
    call :time2timestamp "%~1" t1
    call :time2timestamp "%~2" t2
    if %t2% gtr %t1% ( set /a "t=t2-t1" ) else ( set /a "t=8640000-t1+t2" )
    if not "%~4"=="" set /a "h=100+ (t/360000)", "m=100+ (t-(h-100)*360000)/6000", "s=100+ (t-(h-100)*360000-(m-100)*6000)/100", "c=100+ (t %% 100)"
    endlocal & (if not "%~3"=="" set "%~3=%t%") & (if not "%~4"=="" set "%~4=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%") & exit /b 0

:time2timestamp timeValue returnVariable
    setlocal enableextensions
    if "%~1"=="" ( set "ts=%time%" ) else ( set "ts=%~1" )
    if "%ts:pm=%"=="%ts%" ( set "adjust=0" ) else ( set "adjust=12" )
    for /f "tokens=1-4 delims=:-.,apm " %%a in ("%ts%") do ( set "h=00%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d")
    set /a "ts=(1%h:~-2%-100+adjust)*360000+(1%m:~-2%-100)*6000+(1%s:~-2%-100)*100+(1%c:~-2%-100)"
    endlocal & set "%~2=%ts%" & exit /b 0

For the play sound part, as far as i know, there is not way of directly playing a custom wav from command line without spawning another process. The usual tools are vlc, wmplayer, a vbs file instancing the wmplayer ocx, sound recorder, powershell, ... All this options are documented in previous questions (just the first) here in stackoverflow or here in superuser.

Expanding one of the alternatives, if you have access to some C compiler (tested with mingw) this code will generate a console tool that calls the PlaySound API function passing the first argument as the file to play.

#include <windows.h>
int main(int argc, char **argv)
{
    if (argc < 2) return 1;
    if (!PlaySound( 
            argv[1], 
            NULL, 
            SND_FILENAME | SND_NODEFAULT | SND_SYNC
       )
    ) return 2;
    return 0;
}

Depending of your configuration you will need to include a reference to the Winmm library to the linker.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Best way to check it 20 seconds have passed is by using the command timeout. The syntax of timeout is:

timeout [TIME]

I recommend by using it like this:

timeout 20 >nul