Batch Script Parameters

Posted on 2007-10-10
Medium Priority
Last Modified: 2010-08-05
I want to be able to process variables from the run prompt of my batch script. Example: pingbatch -n 4 -w 1000 -i 32 where = destination address, where -n = number of times to ping, where -w = time to wait in milliseconds and where -i = Time to Live.

I want full error checking so that it does not matter what order they are entered in (except the destination address, this should always be %1) then for example %2 could equal -n, -w or -i, %3 would be the result for %2 which could be -n, -w, -i and so on.

What is the easiest way to build this error checking into my batch file so that I am then able to process the following command with the correct parameters: ping -w 1000 -n 4 -i 32 but using variables taken from the %1, %2, %3, etc.
Question by:Bulldog98
  • 2

Expert Comment

ID: 20049003
Are you mandating that the argument is prefixed with the switch desgnator, ie. the user supplies '-n 4' not just 4?
LVL 30

Accepted Solution

SteveGTR earned 2000 total points
ID: 20049229
You could do something like this:

@echo off


set ipAddr=%~1
set nParm=
set wParm=
set iParm=

if "%ipAddr%"=="" goto USAGE
if "%ipAddr:~0,1%"=="-" echo IP address must be specified first&goto USAGE



if "%~1"=="" goto START

set parm=%~1

if not "%parm:~0,1%"=="-" echo Invalid parameters&goto USAGE
if not "%parm:~2,1%"=="" echo Invalid parameters&goto USAGE

if "%parm:~1,1%"=="n" set nParm=%~2&shift&goto NEXTPARM
if "%parm:~1,1%"=="w" set wParm=%~2&shift&goto NEXTPARM
if "%parm:~1,1%"=="i" set iParm=%~2&shift&goto NEXTPARM

echo Invalid parameters&goto USAGE


set cmdStr=ping %ipAddr%

if not "%nParm%"=="" set cmdStr=%cmdStr% -n %nParm%
if not "%wParm%"=="" set cmdStr=%cmdStr% -w %wParm%
if not "%iParm%"=="" set cmdStr=%cmdStr% -i %iParm%

echo %cmdStr%

goto :EOF


echo Usage: %~0 -n 4 -w 1000 -i 32
echo where = destination address, where -n = number of times to ping, where -w = time to wait in milliseconds and where -i = Time to Live.

goto :EOF

Author Comment

ID: 20049417
Dude, you're a genius. :) Works perfect. Thanks for you're help.
LVL 30

Expert Comment

ID: 20049445
Love answering questions that are so wonderfully asked. Everything required and necessary to answer the question. Thanks :)

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question