Avatar of afrpa
afrpa
Flag 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.
Microsoft DOSWindows 7

Avatar of undefined
Last Comment
Steve Knight

8/22/2022 - Mon
Amick

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".
jsdray

the -s is not needed... it will just show up as your %1 as Arnick's example will show you...
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Amick

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
afrpa

ASKER
Thanks, I appreciate the help,  It has been very insightful towards the situation.
Steve Knight

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

Steve Knight

Hmm, looks like I posted a bit late there!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.