Take the 2-minute tour ×

I needed to pass id and password to a cmd (or bat) file at the time of running rather than hardcoding them into the file.

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt
share|improve this question
Adding an @ character in front of the echo off command (i.e., @echo off) will avoid the annoying "echo off" echo in the output from your batch file. –  dave-holm Sep 23 '11 at 1:51
For "all the rest" see Greg Hegill's comment at how to get batch file parameters from Nth position on? –  matt wilkie Jan 7 at 21:04
add comment

10 Answers

Another useful tip is to use %* to mean "all the rest". For example,

echo off
fake-command /u %1 /p %2 %*

When you run:

test-command admin password foo bar

the above batch file will run:

fake-command /u admin /p password foo bar

Edit: Actually, %* means "all", so one would actually need to use shift to do the above:

echo off
set arg1=%1
set arg2=%2
fake-command /u %arg1% /p %arg2% %*

I may still have the syntax slightly wrong, but this is the general idea. It's been a very long time since I've written a batch file, and my brain keeps thinking "shell script"!

share|improve this answer
%* actually expands to all parameters regardless of shift. So even after the two shifts you would still have the first two arguments in %*. You can use something like this: stackoverflow.com/questions/761615/… to get a variable that contains everything but the first n parameters. –  Јοеу May 1 '09 at 20:52
Please note that %* does not work everywhere! For instance, it does not work with DOSBox 0.73 (maybe this is a bug that should be reported). –  Denilson Sá Feb 8 '10 at 2:50
It's not a bug because %* never worked in MS-DOS or Win9x in the first place. –  Kef Schecter Nov 23 '11 at 4:54
add comment
up vote 49 down vote accepted

Here's how I do it.

@fake-command /u %1 /p %2

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt

The %1 applies to the first parameter the %2 (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.

share|improve this answer
You left this very much not clear. DId you mean $1 is the parameter 1, $2 is the parameter two? Where the Batch.bat file has simply a command called telnet %1 %2 %3 and we use it as Batch.bat Keng stackover.com 23 –  YumYumYum Jul 19 '11 at 14:50
@89899.3K I said it was a tricky part 80) –  Keng Jul 19 '11 at 17:25
add comment

If you want to intelligently handle missing parameters you can do something like:

IF %1.==. GOTO No1
IF %2.==. GOTO No2
... do stuff...

  ECHO No param 1
  ECHO No param 2

share|improve this answer
add comment

Yep, and just don't forget to use variables like %%1 when using if and for and the gang.

If you forget the double %, then you will be substituting in (possibly null) command line arguments and you will receive some pretty confusing error messages.

share|improve this answer
%% is only for if and for ? –  Royi Namir Oct 27 '12 at 17:01
It's worse than that - %% is used to prefix variables and command line parameters inside batch files. But when you use these commands from the command line, you use only % to prefix. Example: inside batch: for %%d in (*) do echo %%d from command line: for %d in (*) do echo %d –  Steve Midgley Jan 16 '13 at 17:08
add comment

Accessing batch parameters can be simple with %1, %2, ... %9 or also %*,
but only if the content is simple.

There is no simple way for complex contents like "&"^&, as it`s not possible to access %1 without producing an error.

set  var=%1
set "var=%1"
set  var=%~1
set "var=%~1"

The lines expands to

set  var="&"&
set "var="&"&"
set  var="&"&
set "var="&"&"

And each line fails, as one of the & is outside of the quotes.

It can be solved with reading from a temporary file a remarked version of the parameter.

@echo off
SETLOCAL DisableDelayedExpansion

for %%a in (1) do (
    set "prompt="
    echo on
    for %%b in (1) do rem * #%1#
    @echo off
) > param.txt

for /F "delims=" %%L in (param.txt) do (
  set "param1=%%L"
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo %%1 is '!param1!'

The trick is to enable echo on and expand the %1 after a rem statement (works also with %2 .. %*).
So even "&"& could be echoed without producing an error, as it is remarked.

But to be able to redirect the output of the echo on, you need the two FOR-LOOPS.

The extra characters * # are used to be safe against contents like /? (would show the help for REM).
Or a caret ^ at the line end could work as a multiline character, even in after a rem.

Then reading the rem parameter output from the file, but carefully.
The FOR /F should work with delayed expansion off, else contents with "!" would be destroyed.
After removing the extra characters in param1, you got it.

And to use param1 in a safe way, enable the delayed expansion.

share|improve this answer
add comment

No need to complicate it. It is simply command %1 %2 parameters, for example

@echo off

xcopy %1 %2 /D /E /C /Q /H /R /K /Y /Z

echo copied %1 to %2


The "pause" displays what the bat has done and waits for you to hit the ANY key. Save that as xx.bat in the Windows folder. To use it type, for example:

xx c:\f\30\*.* f:\sites\30

This bat takes care of all the necesary parameters, like copying only files, that are newer, etc. I have used it since before Windows. If you like seeing the names of the files, as they are being copied, leave out the Q parameter.

share|improve this answer
add comment

to refer to a set variable in command line you would need to use I.E. %a% so

      set a=100 
      echo %a% | 
      output = 100 || 

*note*this works for windows 7 pro.

share|improve this answer
add comment



IF "%1"=="" GOTO Continue

• • Here your batch file handles %1 •




Note: IF "%1"=="" will cause problems if %1 is enclosed in quotes itself.

In that case, use IF [%1]==[] or, in NT 4 (SP6) and later only, IF "%~1"=="" instead.

share|improve this answer
add comment

make new batch file (exp: openclass.bat) and write line in file:

java %~n1

then place the batch file in let say system32 folder, go to your java class file, right click, Properties, Open with..., then find your batch file, select him and thats that... works for me.

PS: i cant find the way when i close the java class to cloce the cmd window? for now...

share|improve this answer
add comment


set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.run "runas /user:domain\admminuser c:\test\rename.bat"



WshShell.SendKeys "adminpassword"

WshShell.SendKeys "{ENTER}"

share|improve this answer
The question was about batch-script and how to access parameters given at the command line. You show how to send a fixed password via sendkey written in VB. Seems not to be an answer to this question –  jeb Oct 17 '13 at 12:11
add comment

Your Answer


