Batch Script Parameters

Posted on 2007-10-10
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
    LVL 9

    Expert Comment

    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

    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

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

    Expert Comment

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now