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

afrpa
afrpa used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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".

Commented:
the -s is not needed... it will just show up as your %1 as Arnick's example will show you...

Author

Commented:
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.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Commented:
Here's a quick example that reads a switch,

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

if /i %arg1%==-s (
	echo -s switch discovered
	if %arg2%. NEQ %blank%. (
		echo do -s things with %arg2%
	) else echo argument required.

) else (
echo NO -s switch found
)

Open in new window

Commented:
if you plan to use switch identifiers like "/c"  then you have to script for them as well.  As far as a batch file goes... they are just another argument.
So to use your example above :
batchfile.bat /c "Win7comp" /t 30 /d "C:\Temp\Test.txt"
 you'd have 6 arguments.... %1 would be /c
%2 would be "Win7comp"
%3 would be /t
...and so on....
You'd have to use logic like Arnick provided or just pass the arguments in the proper order and not worry about switches...

batchfile.bat "Win7comp" 30 "C:\Temp\Test.txt"

Author

Commented:
Thanks, I appreciate the help,  It has been very insightful towards the situation.

Commented:
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

Commented:
Hmm, looks like I posted a bit late there!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial