Link to home
Start Free TrialLog in
Avatar of afrpa
afrpaFlag for United States of America

asked on

Command Line Batch File (External Parameters [-switches])

I have been writing batch files, and was wondering if there is a way to integrate user input by adding custom switches to batch files, so that it picks up the variables when running the batch file.  

I know you can use the set /p to prompt the user for input, but I want to be able to grab the parameters from the single command that the user inputs outside of the batch file.

For example >    From command line.

C:\> batchfile.bat -s "somevariable"

I want it to pass that "somevariabe" to the program when it runs the batch file, but don't know the syntax on how to make this opperational.

Any help/advice is appreciated.  Thanks.
Avatar of Amick
Amick
Flag of United States of America image

The command line arguments are numbered from %0 and may be used directly or assigned to variables:

echo off
set arg1=%1
set arg2=%2
echo %0
echo %arg1%
echo %arg2%

Open in new window


You may also want to be aware of the command "shift".
the -s is not needed... it will just show up as your %1 as Arnick's example will show you...
Avatar of afrpa

ASKER

Is this is same kind of concept used for things such as when you use the command:

CMD:>  defrag C:\ /A /U

Where the commands that are given to the program are then read into the process of the program?

What I am trying to do is set static switches for specific variables such as
batchfile.bat /c "Win7comp" /t 30 /d "C:\Temp\Test.txt"

where:   /c would be the computername
/t would be the time to wait to process the command
/d would be the directory to save the output to
and the user can specify  them during the time of inputting the command.

Sorry if the question is a bit confusing, I am not really sure how to word it correctly.

Thank you for your quick feed back, I appreciate all the help.
ASKER CERTIFIED SOLUTION
Avatar of Amick
Amick
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of afrpa

ASKER

Thanks, I appreciate the help,  It has been very insightful towards the situation.
A useful one here btw is "SHIFT".  It moves the paramters up one:

i.e. you can do something like this to loop through the params in any order, e.g. my scripts here:

http://scripts.dragon-it.co.uk/links/batch-named-parameters
http://scripts.dragon-it.co.uk/links/batch-count-parameters


@echo off
SETLOCAL

REM Check if any parameters have been given.
if "%*"=="" (
  echo Usage:
  echo -s "this"
  echo -a = that
  exit /b
)

echo The parameters for %~f0 are %*
echo.

REM loop through all parameters
:loop
  if /i "%~1"=="-s" (
    echo Deal with "%~2" as that part of the parameter
    Set OptionS=%~2
    SHIFT
  )
  if /i "%~2"=="-a" set OptionA=Yes
    echo Deal with a which does not have a parameter
  )
  SHIFT
if NOT "%~1"=="" goto loop

echo.

if NOT "%OptionS%"=="" echo Option S was chosen with %OptionS%

if NOT "%OptionA%"=="" echo Option A was chosen
echo.

ENDLOCAL

Open in new window

Hmm, looks like I posted a bit late there!