Batch Script that accepts Named Arguments

in #batch7 years ago (edited)

This post might help someone that wants to create a Batch or Command script that accepts named arguments. 

Quick Introduction

Normally, when you pass arguments or parameters to a batch script, they are in an order and are assigned a number, for example:

MyScript.cmd "Some Value" ABC 123

You can get the value of any argument using a % followed by it's numerical position. The first item passed is always %1 the second item is always %2 and so on.

%1 = "Some Value"

%2 = ABC

%3 = 123

%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255) , but only arguments %1 to %9 can be referenced by number. 

My Scenario

To demonstrate my point, I made up this scenario.  Let's say for some reason, when you launch Internet Explorer, you want to pass the URL to the Browser, but also have the option to pop-up an informative message before the browser starts and in certain cases have the ability to not launch the browser at all, but just display an informative pop-up message.  

Batch Script File

On a side note, I use Notepad++ to edit my Batch scripts.  It is also very handy for many other types of scripts because it does Syntax Highlighting. 

Create a file called launch-IE.cmd and copy the script code that is listed below into the file and save it to a folder called D:\Scripts

Note: The batch script contains remarks to help you.

ECHO OFF
CLS

:: === Variables
:: --- Script Runtime folder

SET mRuntime=D:\Scripts

:: --- Creates a Random named CMD file in Users TEMP folder
SET mFilename=%TEMP%\%RANDOM%
SET mCMDFile="%mFilename%.CMD"

:: === Check CMD FIle Arguments
:: --- Ensure there are Arguments

IF [%1]==[] GOTO
:ARG_NONE

:: --- Set Variables from Arguments
FOR %%A IN (%*) DO (SET %%A)

:: --- Validate REQUIRED Arguments
IF /I [%-URL%] EQU [] GOTO
:ARG_REQUIRED

:: --- Validate OPTIONAL Arguments
:: --- To show an additional pop-up message before program launches

IF /I "%-ShowMsg%" EQU "" (
  SET mMsgShow=FALSE
) ELSE (
  IF NOT EXIST "%-ShowMsg%" ECHO ERROR: Cannot find Message file & GOTO
:SHOW_SYNTAX
  SET mMsgShow=TRUE
  SET mMsgCMD=START "" /D "%mRuntime%" /LOW /WAIT "%mRuntime%\popupMsg.exe" -i "%-ShowMsg%"
)

:: --- Define the Application you want to Launch
SET mRunFolder=%ProgramFiles(x86)%\Internet Explorer
SET mRunEXE=%mRunFolder%\iexplore.exe
SET mRunCMD=START "" /D "%mRunFolder%" "%mRunEXE%" %-URL%

:: === Start
:START
:: --- Create CMD to Launch Application
> %mCMDFile% ECHO ECHO OFF
IF /I [%mMsgShow%] EQU [TRUE] ECHO %mMsgCMD% >> %mCMDFile%
IF /I [%-SkipLaunch%] NEQ [TRUE] ECHO %mRunCMD% >> %mCMDFile%

:: --- Launch
START "" /MIN C:\Windows\System32\CMD.EXE /c "%mCMDFile%"

:END
EXIT

:: === SYNTAX Definition for this CMD File
:ARG_NONE
:: --- Argument Error
ECHO ERROR: No COMMAND Line arguments found...
ECHO Arguments List: %*
GOTO
:SHOW_SYNTAX

:ARG_REQUIRED
ECHO ERROR: One or more REQUIRED arguments are missing..
ECHO Arguments List: %*
GOTO
:SHOW_SYNTAX

:SHOW_SYNTAX
ECHO:
ECHO Syntax........: %~n0.CMD "-URL=url" "-ShowMsg=FullPathFile" "-SkipLaunch=TRUE"
ECHO:
ECHO Named Argument: -URL : This is a REQUIRED argument.
ECHO               : "-URL=https://bing.com"
ECHO:
ECHO Named Argument: -ShowMsg : Show Message: OPTIONAL
ECHO               : "-ShowMsg=Full Path with space\Filename.ini"
ECHO:
ECHO Named Argument: -SkipLaunch : Skip launching Main Application: OPTIONAL
ECHO               : "-SkipLaunch=TRUE"
ECHO:
ECHO Press any key to close window...
PAUSE >NUL
EXIT /B 1

Create a Desktop Shortcut

Target: Use any one of these examples

D:\Scripts\launch-IE.cmd "-URL=bing.ca"
D:\Scripts\launch-IE.cmd "-URL=bing.ca" "-ShowMsg=popupMsg.ini"
D:\Scripts\launch-IE.cmd "-URL=bing.ca" "-ShowMsg=popupMsg.ini" "-SkipLaunch=TRUE"

Start in: D:\Scripts

Click on the [Change Icon...] button to select an Icon from: %ProgramFiles(x86)%\Internet Explorer\iexplore.exe


Note: popupMsg.exe is not part of Windows.  It is just a utility I wrote to help me in my day to day activities in dealing with application outages.

I have zipped the Launch-IE.cmd,  popupMsg.exe and popupMsg.ini files for you to download if you wish to do that.  [Download]

Output Example:

Thank you for reading my post.  Happy scripting...


Sort:  

Hello @captainm, upv0t3
This is a free service for new steemit users, to support them and motivate them to continue generating valuable content for the community.
<3 This is a heart, or an ice cream, you choose.

:)


R4ND0M:
1379 7962 3841 1311
8605 6080 8917 4882
9527 5393 3331 2883
1239 6089 1491 5402

Congratulations @captainm! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @captainm! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.19
JST 0.034
BTC 89752.15
ETH 3297.99
USDT 1.00
SBD 3.02