From JJ :
>For instance, say I wanted to allow switches such as /f
>(force) and /s
>(silent) and others for controlling the actions within
>the batch
>itself, without requiring them to be in a strict order
>so that they
>appear within %1, %2, etc. Is there a way to parse
>them?
I've been using this algorithm successfully for many years:
@Echo OFF
SetLocal EnableExtensions
:: Remove all '$' variables from local environment:
For /F "tokens=1 delims==" %%a in ('Set "$" 2^>NUL:') Do Set "%%a="
For /F "tokens=1 delims==" %%a in ('Set "#" 2^>NUL:') Do Set "%%a="
:: Script name for error output:
Set "$ME=%~n0"
:: Parse all arguments:
:args
:: I use '$' alone as a temporary variable throughout.
Set "$=%1"
:: Use 'IF /I' so they are not case sensative.
If /I "%$%" EQU "/S" (
Set "$Silent=1"
Shift
) Else If /I "%$%" EQU "/SILENT" (
Set "$Silent=1"
Shift
) Else If /I "%$%" EQU "/F" (
Set "$Force=1"
Shift
) Else If /I "%$%" EQU "/FORCE" (
Set "$Force=1"
Shift
) Else If /I "%$:~0,1%" EQU "/" (
(Echo %$ME%: Aborting, unknown parameter '%$%'.)>&2
Goto :EOF
) Else If DEFINED $argFile (
(Echo %$Me%: Aborting, too many filenames.)>&2
Goto :EOF
) Else (
Set "$argFile=%~f1"
Shift
)
IF "%1" NEQ "" Goto :args
:: For demonstration, view all script variables:
Set $
Set #
I use the prefix '$' for variable variables and the
prefix '#' for constant variables.
All parameters are parsed, case not significant, and
unknown parameters cause an abort. In the above demo an
argument not prefixed with '/' is assumed to be the name
of a file which will be used in the script.
Frank
--
Sent from an Android