I have a Windows batch script that operates in one of three modes, depending on the number of command-line arguments:
1 command-line argument: plays named .wav file
2 command-line arguments: runs command specified by first command-line argument and then plays .wav file specified via second command-line argument
3 command-line arguments: runs command specified by first command-line argument and then plays .wav file specified via second or third command-line argument, depending on whether the command was successful
My issue is with the third mode: The script always plays the first sound, regardless of whether the command was successful. Any suggestions will be appreciated.
@ECHO OFF
Setlocal disableDelayedExpansion
:: OVERVIEW
:: This script optionally runs a command and then uses Eli Fulkerson's
:: `sounder.exe` to play the specified or default sound.
:: The Media environment variable must be defined and contain the path of a
:: folder containing one or more .wav files.
:: REFERENCE
:: https://www.elifulkerson.com/projects/commandline-wav-player.php
:: USAGE CASE 1: NO ARGUMENTS
:: If the script is invoked without arguments, it plays the default sound file
:: `beep-05.wav`.
:: USAGE CASE 2: ONE ARGUMENT
:: The single argument is the name of a sound file, minus the .wav extension.
:: sound.bat will play this sound file. Example:
:: sound meow1
:: USAGE CASE 3: TWO ARGUMENTS
:: The first argument contains a command to be run; the second argument is the
:: name of a sound file, minus the .wav extension. sound.bat will play the
:: sound after the command finishes running, regardless of exit code. Example:
:: sound "dir C:\Phillip" meow1
:: USAGE CASE 4: THREE ARGUMENTS
:: The first argument contains a command to be run; the second and third
:: arguments name sound files, minus the .wav extension to be played if the
:: executes successfully or fails, respectively.
:: sound "dir C:\Phillip" meow1 meow2
:: NOTE
:: It is not currently possible to use this scripts to play sound files that are
:: located outside the Media folder.
:: AUTHOR
:: Phillip M. Feldman
:: REVISION HISTORY
:: June 5, 2020, Phillip M. Feldman: Initial version.
If not defined Media (
msg "%username%" The Media environment variable is not defined!
GOTO :EOF
)
If not EXIST %Media% (
msg "%username%" The Media folder '%Media%' does not exist!
GOTO :EOF
)
If '%1' == '' (
:: No command-line arguments.
sounder %Media%\beep-05.wav
GOTO :EOF
)
If '%2' == '' (
:: One command-line argument.
sounder %Media%\%1%.wav
GOTO :EOF
)
If '%3' == '' (
:: Two command-line arguments.
%~1
sounder %Media%\%2%.wav
GOTO :EOF
)
If '%4' == '' (
:: Three command-line arguments.
%~1
If %ERRORLEVEL% == 0 (
sounder %Media%\%2%.wav
) else (
sounder %Media%\%3%.wav
)
GOTO :EOF
)