?
Solved

Batch Script Parameters

Posted on 2007-10-10
4
Medium Priority
?
1,195 Views
Last Modified: 2010-08-05
I want to be able to process variables from the run prompt of my batch script. Example: pingbatch 192.168.1.1 -n 4 -w 1000 -i 32 where 192.168.1.1 = 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 192.168.1.1 but using variables taken from the %1, %2, %3, etc.
0
Comment
Question by:Bulldog98
  • 2
4 Comments
 
LVL 9

Expert Comment

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

Accepted Solution

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

@echo off

setlocal

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

:NEXTPARM

shift

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

:START

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

:USAGE

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

goto :EOF
0
 

Author Comment

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

Expert Comment

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

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