1

I need to pass command arguments with a batch file. How would I do that? I have a little program set up but I don't know how to get the command arguments.. or really how to make a command.

@echo OFF
title Name pl0x
color 0a
:start
set INPUT=
set /P INPUT= %=%
IF "%INPUT%"=="/mynameis" (
goto :init
) ELSE (
goto :start
)
:init
Pause&Exit
REM Here, I'd print the name, like this: echo.Hello, %name%
REM I just don't have the variable. And I don't know how to set it.
1
  • If I resolved this or your last question remember to use the ACCEPT button to accept my answers, thankyou. Commented Apr 25, 2013 at 21:59

2 Answers 2

2

Arguments are done with %1 %2, etc

So if you have mynameis.bat %1 will contain test

Sign up to request clarification or add additional context in comments.

3 Comments

any idea how I can use this in a command? Like I'd want the user to do /mynameis Name
Are you talking about named arguments? Or is mynameis a program or batch file?
Named arguments... I think, yes. Because mynameis is not a program/batch file
1

An argument is a delimited word by a space wich you can use like a parameter in a application.

Then if call a script like this:

Script.Bat word1 word2 word3_word4-word5

The arguments are:

Argument 1 = "word1"
Argument 2 = "word2"
Argument 3 = "word3_word4-word5" (Because any space).

In Batch arguments are stored in special vars from %1 to %255 where %1 equals to the "Argument 1".

%0 equals to the current script name or procedure name.

%* joins all th arguments

..And the special modifier ~ expand the argument without double quotes.

Then if we have this code:

@Echo OFF
If /I "%~1" EQU "/Print" (Echo: You called the function "%~1" with the value: "%~2"))

We are checking if argument 1 is equals to "/Print", then if is equals prints the value for the switch, the second argument.

Then you can call your bat like this:

Script.bat /Print Hello!

Learn more about Batch arguments here: http://ss64.com/nt/syntax-args.html

2 Comments

%0 to %9 is the range - they do not go up to %255. You have to use SHIFT to access any parameters past the 9th one.
That's the range to print/use them, but what I said is they are stored up to number %255 (%10-%255 are imaginary, hiddenly, but usable with shift like you said), you can't store/use/shift more than 255 args.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.