Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I need 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
4  
For "all the rest" see Greg Hegill's comment at how to get batch file parameters from Nth position on? – matt wilkie Jan 7 '14 at 21:04
    
I have an environment startup script that will push my username/password into environment variables... so that I don't have to type them out each time... I'm using bash most of the time though (linux, mac and windows), and need to use it for proxy configs in scripts, etc for work. – Tracker1 Oct 7 '15 at 4:35

12 Answers 12

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

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

When you run:

test-command admin password foo bar

the above batch file will run:

fake-command /u admin /p password foo bar

I may have the syntax slightly wrong, but this is the general idea.

share|improve this answer
72  
%* 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. – Joey 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á Maia Feb 8 '10 at 2:50
21  
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
    
Missing "@" after "echo"... – wrivas Jul 2 '15 at 15:22
10  
@wrivas Why after echo? I'd put it before... – glglgl Sep 10 '15 at 10:15
up vote 168 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
8  
If you're as dumb as me, your mind was looking for echo %1 %2 and was thrown off by the non cut-and-pasteable simplest case with a @ and a fake-command with params, thinking we'd get fake-command.bat's contents later (in which case, the overcomplicated fake-command.bat might have echo %2 %4 to ignore the param names). Wrong, doofus. TL;DR: Don't be as dumb as me. 1. echo echo %1 %2 > test.bat 2. test word1 word2. 3. Profit. – ruffin Mar 5 '15 at 18:26
    
for the third point 3.del test.bat – Mohamed Iqzas Oct 21 '16 at 14:49

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

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

:No1
  ECHO No param 1
GOTO End1
:No2
  ECHO No param 2
GOTO End1

:End1
share|improve this answer

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

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

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

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
19  
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
3  
@SteveMidgley I upvoted your comment probably a year or so ago. I then promptly forgot about it and just today I was trying and staring confusingly at my for loop in the command line and wondering why it farted and did nothing else. So here's another virtual upvote from me. I'll be back in another year or so when I hit the same issue again. – icc97 Dec 29 '16 at 21:53

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

pause

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
@ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
SHIFT
GOTO Loop
:Continue

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
FOR %%A IN (%*) DO (
    REM Now your batch file handles %%A instead of %1
    REM No need to use SHIFT anymore.
    ECHO %%A
)

This loops over the batch parameters (%*) either they are quoted or not, then echos each parameter.

share|improve this answer

Let's keep this simple.

Here is the .cmd file.

@echo off
rem this file is named echo_3params.cmd
echo %1
echo %2
echo %3
set v1=%1
set v2=%2
set v3=%3
echo v1 equals %v1%
echo v2 equals %v2%
echo v3 equals %v3%

Here are 3 calls from the command line.

C:\Users\joeco>echo_3params 1abc 2 def  3 ghi
1abc
2
def
v1 equals 1abc
v2 equals 2
v3 equals def

C:\Users\joeco>echo_3params 1abc "2 def"  "3 ghi"
1abc
"2 def"
"3 ghi"
v1 equals 1abc
v2 equals "2 def"
v3 equals "3 ghi"

C:\Users\joeco>echo_3params 1abc '2 def'  "3 ghi"
1abc
'2
def'
v1 equals 1abc
v2 equals '2
v3 equals def'

C:\Users\joeco>
share|improve this answer

I wrote a simple read_params script that can be called as a function (or external .bat) and will put all variables into the current environment. It won't modify the original parameters because the function is being called with a copy of the original parameters.

For example, given the following command:

myscript.bat some -random=43 extra -greeting="hello world" fluff

myscript.bat would be able to use the variables after calling the function:

call :read_params %*

echo %random%
echo %greeting%

Here's the function:

:read_params
if not %1/==/ (
    if not "%__var%"=="" (
        if not "%__var:~0,1%"=="-" (
            endlocal
            goto read_params
        )
        endlocal & set %__var:~1%=%~1
    ) else (
        setlocal & set __var=%~1
    )
    shift
    goto read_params
)
exit /B

Limitations

  • Cannot load arguments with no value such as -force. You could use -force=true but I can't think of a way to allow blank values without knowing a list of parameters ahead of time that won't have a value.

Changelog

  • 2/18/2016
    • No longer requires delayed expansion
    • Now works with other command line arguments by looking for - before parameters.
share|improve this answer
    
Been looking at using this method as I would like to pass arguments into a batch file in this manner. However I notice that after the variables are set, even after exiting the batch file the parameters are still set in the cmd if accessed and the batch has ended, they are not restored to their previous state. Should this method handle that situation? – Jono_2007 Sep 14 '16 at 11:53

To refer to a set variable in command line you would need to use " %a% " so for example:

      set a=100 
      echo %a%  
      output = 100 

Note: This works for Windows 7 pro.

share|improve this answer

Make a new batch file (example: openclass.bat) and write this line in the file:

java %~n1

Then place the batch file in, let's say, the system32 folder, go to your Java class file, right click, Properties, Open with..., then find your batch file, select it and that's that...

It works for me.

PS: I can't find a way to close the cmd window when I close the Java class. For now...

share|improve this answer
    
You can close the cmd window after the process has closed using the following command in a batch script: start /wait java %~n1 – Paint_Ninja Jul 31 '16 at 16:38

protected by lpapp Jun 22 '14 at 20:19

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.