| (On fail) conditional operators:

问题描述 投票:0回答:1

With two minor changes, the script (see below) now works. I want to thank everyone who helped or tried to help.

Here's an example using the method I proposed via comment under your own answer, and with some additional validation included:

@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
)
windows batch-file command-line-arguments
1个回答
1
投票

2个命令行参数:运行第一个命令行参数指定的命令,然后播放通过第二个命令行参数指定的.wav文件。

If "%~4" == "" (

   :: Three command-line arguments.
   %~1

   If "%ERRORLEVEL%" == "0" (
      sounder "%Media%\%~2.wav"
   ) else (
      sounder "%Media%\%~3.wav"
   )
   GOTO :EOF
)

3个命令行参数:运行第一个命令行参数指定的命令,然后播放通过第二个或第三个命令行参数指定的.wav文件,取决于命令是否成功。

(If Not "%~3" == "" %~1) && sounder "%Media%\%~2.wav" || sounder "%Media%\%~3.wav"
我的问题是第三种模式。脚本总是播放第一个声音, 不管命令是否成功。 任何建议都将被感激。

0
投票

@ECHO OFF
Setlocal enableDelayedExpansion

:: 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 1 (
      sounder %Media%\%3%.wav
   ) else (
      sounder %Media%\%2%.wav
   )
   GOTO :EOF
)
我有一个Windows批处理脚本,根据命令行参数的数量,以三种模式之一运行。1个命令行参数: 播放名为.wav的文件 2个命令行参数: 运行...

0
投票

使用&&(成功时)和...

© www.soinside.com 2019 - 2024. All rights reserved.