<p>I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file.</p>

<p>Here's what the command line looks like:</p>

<pre><code>test.cmd admin P@55w0rd &gt; test-log.txt
</code></pre>

[{'answer_id': 26556, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 6, 'selected': False, 'text': "<p>Yep, and just don't forget to use variables like <code>%%1</code> when using <code>if</code> and <code>for</code> and the gang.</p>\n\n<p>If you forget the double <code>%</code>, then you will be substituting in (possibly null) command line arguments and you will receive some pretty confusing error messages.</p>\n"}, {'answer_id': 26702, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 10, 'selected': False, 'text': '<p>Another useful tip is to use <code>%*</code> to mean &quot;all&quot;. For example:</p>\n<pre><code>echo off\nset arg1=%1\nset arg2=%2\nshift\nshift\nfake-command /u %arg1% /p %arg2% %*\n</code></pre>\n<p>When you run:</p>\n<pre><code>test-command admin password foo bar\n</code></pre>\n<p>The above batch file will run:</p>\n<pre><code>fake-command /u admin /p password admin password foo bar\n</code></pre>\n<p>I may have the syntax slightly wrong, but this is the general idea.</p>\n'}, {'answer_id': 26742, 'author': 'thelsdj', 'author_id': 163, 'author_profile': 'https://Stackoverflow.com/users/163', 'pm_score': 7, 'selected': False, 'text': '<p>If you want to intelligently handle missing parameters you can do something like:</p>\n\n<pre><code>IF %1.==. GOTO No1\nIF %2.==. GOTO No2\n... do stuff...\nGOTO End1\n\n:No1\n  ECHO No param 1\nGOTO End1\n:No2\n  ECHO No param 2\nGOTO End1\n\n:End1\n</code></pre>\n'}, {'answer_id': 92057, 'author': 'Keng', 'author_id': 730, 'author_profile': 'https://Stackoverflow.com/users/730', 'pm_score': 9, 'selected': True, 'text': "<p>Here's how I did it:</p>\n\n<pre><code>@fake-command /u %1 /p %2\n</code></pre>\n\n<p>Here's what the command looks like:</p>\n\n<pre><code>test.cmd admin P@55w0rd &gt; test-log.txt\n</code></pre>\n\n<p>The <code>%1</code> applies to the first parameter the <code>%2</code> (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.</p>\n"}, {'answer_id': 4913598, 'author': 'SvenVP', 'author_id': 605296, 'author_profile': 'https://Stackoverflow.com/users/605296', 'pm_score': 2, 'selected': False, 'text': "<p>Make a new batch file (example: openclass.bat) and write this line in the file:</p>\n\n<pre><code>java %~n1\n</code></pre>\n\n<p>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...</p>\n\n<p>It works for me.</p>\n\n<p>PS: I can't find a way to close the cmd window when I close the Java class. For now...</p>\n"}, {'answer_id': 5493124, 'author': 'jeb', 'author_id': 463115, 'author_profile': 'https://Stackoverflow.com/users/463115', 'pm_score': 7, 'selected': False, 'text': '<p>Accessing batch parameters can be simple with %1, %2, ... %9 or also %*,<br>\nbut only if the content is simple.  </p>\n\n<p>There is no simple way for complex contents like <code>"&amp;"^&amp;</code>, as it\'s not possible to access %1 without producing an error.  </p>\n\n<pre><code>set  var=%1\nset "var=%1"\nset  var=%~1\nset "var=%~1"\n</code></pre>\n\n<p>The lines expand to  </p>\n\n<pre><code>set  var="&amp;"&amp;\nset "var="&amp;"&amp;"\nset  var="&amp;"&amp;\nset "var="&amp;"&amp;"\n</code></pre>\n\n<p>And each line fails, as one of the <code>&amp;</code> is outside of the quotes.</p>\n\n<p>It can be solved with reading from a temporary file a <strong>remarked</strong> version of the parameter.</p>\n\n<pre><code>@echo off\nSETLOCAL DisableDelayedExpansion\n\nSETLOCAL\nfor %%a in (1) do (\n    set "prompt="\n    echo on\n    for %%b in (1) do rem * #%1#\n    @echo off\n) &gt; param.txt\nENDLOCAL\n\nfor /F "delims=" %%L in (param.txt) do (\n  set "param1=%%L"\n)\nSETLOCAL EnableDelayedExpansion\nset "param1=!param1:*#=!"\nset "param1=!param1:~0,-2!"\necho %%1 is \'!param1!\'\n</code></pre>\n\n<p>The trick is to enable <code>echo on</code> and expand the %1 after a <code>rem</code> statement (works also with <code>%2 .. %*</code>).<br>\nSo even <code>"&amp;"&amp;</code> could be echoed without producing an error, as it is remarked.  </p>\n\n<p>But to be able to redirect the output of the <code>echo on</code>, you need the two for-loops.</p>\n\n<p>The extra characters <code>* #</code> are used to be safe against contents like <code>/?</code> (would show the help for <code>REM</code>).<br>\nOr a caret ^ at the line end could work as a multiline character, even in after a <code>rem</code>.  </p>\n\n<p>Then reading the <strong>rem parameter</strong> output from the file, but carefully.<br>\nThe FOR /F should work with delayed expansion off, else contents with "!" would be destroyed.<br>\nAfter removing the extra characters in <code>param1</code>, you got it.</p>\n\n<p>And to use <code>param1</code> in a safe way, enable the delayed expansion.</p>\n'}, {'answer_id': 10219730, 'author': 'DearWebby', 'author_id': 1342715, 'author_profile': 'https://Stackoverflow.com/users/1342715', 'pm_score': 6, 'selected': False, 'text': '<p>There is no need to complicate it. It is simply command %1 %2 parameters, for example,</p>\n\n<pre><code>@echo off\n\nxcopy %1 %2 /D /E /C /Q /H /R /K /Y /Z\n\necho copied %1 to %2\n\npause\n</code></pre>\n\n<p>The "pause" displays what the batch file has done and waits for you to hit the ANY key. Save that as xx.bat in the Windows folder.</p>\n\n<p>To use it, type, for example:</p>\n\n<pre><code>xx c:\\f\\30\\*.* f:\\sites\\30\n</code></pre>\n\n<p>This batch file takes care of all the necessary 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 <code>Q</code> parameter.</p>\n'}, {'answer_id': 19985230, 'author': 'CMS_95', 'author_id': 2358955, 'author_profile': 'https://Stackoverflow.com/users/2358955', 'pm_score': 3, 'selected': False, 'text': '<p>To refer to a set variable in command line you would need to use <code>%a%</code> so for example:</p>\n\n<pre><code>set a=100 \necho %a%  \nrem output = 100 \n</code></pre>\n\n<p>Note: This works for Windows 7 pro.</p>\n'}, {'answer_id': 22397352, 'author': 'rightcodeatrighttime', 'author_id': 3414206, 'author_profile': 'https://Stackoverflow.com/users/3414206', 'pm_score': 5, 'selected': False, 'text': '<pre><code>@ECHO OFF\n:Loop\nIF "%1"=="" GOTO Continue\nSHIFT\nGOTO Loop\n:Continue\n</code></pre>\n\n<p>Note:   IF <code>"%1"==""</code> will cause problems if <code>%1</code> is enclosed in quotes itself.</p>\n\n<p>In that case, use <code>IF [%1]==[]</code> or, in NT 4 (SP6) and later only, <code>IF "%~1"==""</code> instead.</p>\n'}, {'answer_id': 27118914, 'author': 'Amr Ali', 'author_id': 4208440, 'author_profile': 'https://Stackoverflow.com/users/4208440', 'pm_score': 5, 'selected': False, 'text': '<pre><code>FOR %%A IN (%*) DO (\n    REM Now your batch file handles %%A instead of %1\n    REM No need to use SHIFT anymore.\n    ECHO %%A\n)\n</code></pre>\n\n<p>This loops over the batch parameters (%*) either they are quoted or not, then echos each parameter.  </p>\n'}, {'answer_id': 34550007, 'author': 'Love and peace - Joe Codeswell', 'author_id': 601770, 'author_profile': 'https://Stackoverflow.com/users/601770', 'pm_score': 5, 'selected': False, 'text': '<p>Let\'s keep this simple.</p>\n\n<p>Here is the .cmd file.</p>\n\n<pre><code>@echo off\nrem this file is named echo_3params.cmd\necho %1\necho %2\necho %3\nset v1=%1\nset v2=%2\nset v3=%3\necho v1 equals %v1%\necho v2 equals %v2%\necho v3 equals %v3%\n</code></pre>\n\n<p>Here are 3 calls from the command line.</p>\n\n<pre><code>C:\\Users\\joeco&gt;echo_3params 1abc 2 def  3 ghi\n1abc\n2\ndef\nv1 equals 1abc\nv2 equals 2\nv3 equals def\n\nC:\\Users\\joeco&gt;echo_3params 1abc "2 def"  "3 ghi"\n1abc\n"2 def"\n"3 ghi"\nv1 equals 1abc\nv2 equals "2 def"\nv3 equals "3 ghi"\n\nC:\\Users\\joeco&gt;echo_3params 1abc \'2 def\'  "3 ghi"\n1abc\n\'2\ndef\'\nv1 equals 1abc\nv2 equals \'2\nv3 equals def\'\n\nC:\\Users\\joeco&gt;\n</code></pre>\n'}, {'answer_id': 35445653, 'author': 'Evan Kennedy', 'author_id': 1572938, 'author_profile': 'https://Stackoverflow.com/users/1572938', 'pm_score': 4, 'selected': False, 'text': '<p>I wrote a simple read_params script that can be called as a function (or external <code>.bat</code>) and will put all variables into the current environment. It won\'t modify the original parameters because the function is being <code>call</code>ed with a copy of the original parameters.</p>\n\n<p>For example, given the following command:</p>\n\n<pre><code>myscript.bat some -random=43 extra -greeting="hello world" fluff\n</code></pre>\n\n<p><code>myscript.bat</code> would be able to use the variables after calling the function:</p>\n\n<pre><code>call :read_params %*\n\necho %random%\necho %greeting%\n</code></pre>\n\n<p>Here\'s the function:</p>\n\n<pre><code>:read_params\nif not %1/==/ (\n    if not "%__var%"=="" (\n        if not "%__var:~0,1%"=="-" (\n            endlocal\n            goto read_params\n        )\n        endlocal &amp; set %__var:~1%=%~1\n    ) else (\n        setlocal &amp; set __var=%~1\n    )\n    shift\n    goto read_params\n)\nexit /B\n</code></pre>\n\n<hr>\n\n<p><strong>Limitations</strong></p>\n\n<ul>\n<li>Cannot load arguments with no value such as <code>-force</code>. You could use <code>-force=true</code> 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.</li>\n</ul>\n\n<hr>\n\n<p><strong>Changelog</strong></p>\n\n<ul>\n<li><strong>2/18/2016</strong>\n\n<ul>\n<li>No longer requires delayed expansion</li>\n<li>Now works with other command line arguments by looking for <code>-</code> before parameters.</li>\n</ul></li>\n</ul>\n'}, {'answer_id': 45070967, 'author': 'kodybrown', 'author_id': 139793, 'author_profile': 'https://Stackoverflow.com/users/139793', 'pm_score': 6, 'selected': False, 'text': "<p>A friend was asking me about this subject recently, so I thought I'd post how I handle command-line arguments in batch files.</p>\n<p>This technique has a bit of overhead as you'll see, but it makes my batch files very easy to understand and quick to implement. As well as supporting the following structures:</p>\n<pre><code>&gt;template.bat [-f] [--flag] [--namedvalue value] arg1 [arg2][arg3][...]\n</code></pre>\n<p>The jist of it is having the <code>:init</code>, <code>:parse</code>, and <code>:main</code> functions.</p>\n<blockquote>\n<p>Example usage</p>\n</blockquote>\n<pre><code>&gt;template.bat /?\ntest v1.23\nThis is a sample batch file template,\nproviding command-line arguments and flags.\n\nUSAGE:\ntest.bat [flags] &quot;required argument&quot; &quot;optional argument&quot;\n\n/?, --help           shows this help\n/v, --version        shows the version\n/e, --verbose        shows detailed output\n-f, --flag value     specifies a named parameter value\n\n&gt;template.bat          &lt;- throws missing argument error\n(same as /?, plus..)\n****                                   ****\n****    MISSING &quot;REQUIRED ARGUMENT&quot;    ****\n****                                   ****\n\n&gt;template.bat -v\n1.23\n\n&gt;template.bat --version\ntest v1.23\nThis is a sample batch file template,\nproviding command-line arguments and flags.\n\n&gt;template.bat -e arg1\n**** DEBUG IS ON\nUnNamedArgument:    &quot;arg1&quot;\nUnNamedOptionalArg: not provided\nNamedFlag:          not provided\n\n&gt;template.bat --flag &quot;my flag&quot; arg1 arg2\nUnNamedArgument:    &quot;arg1&quot;\nUnNamedOptionalArg: &quot;arg2&quot;\nNamedFlag:          &quot;my flag&quot;\n\n&gt;template.bat --verbose &quot;argument #1&quot; --flag &quot;my flag&quot; second\n**** DEBUG IS ON\nUnNamedArgument:    &quot;argument #1&quot;\nUnNamedOptionalArg: &quot;second&quot;\nNamedFlag:          &quot;my flag&quot;\n</code></pre>\n<blockquote>\n<p>template.bat</p>\n</blockquote>\n<pre><code>@::!/dos/rocks\n@echo off\ngoto :init\n\n:header\n    echo %__NAME% v%__VERSION%\n    echo This is a sample batch file template,\n    echo providing command-line arguments and flags.\n    echo.\n    goto :eof\n\n:usage\n    echo USAGE:\n    echo   %__BAT_NAME% [flags] &quot;required argument&quot; &quot;optional argument&quot; \n    echo.\n    echo.  /?, --help           shows this help\n    echo.  /v, --version        shows the version\n    echo.  /e, --verbose        shows detailed output\n    echo.  -f, --flag value     specifies a named parameter value\n    goto :eof\n\n:version\n    if &quot;%~1&quot;==&quot;full&quot; call :header &amp; goto :eof\n    echo %__VERSION%\n    goto :eof\n\n:missing_argument\n    call :header\n    call :usage\n    echo.\n    echo ****                                   ****\n    echo ****    MISSING &quot;REQUIRED ARGUMENT&quot;    ****\n    echo ****                                   ****\n    echo.\n    goto :eof\n\n:init\n    set &quot;__NAME=%~n0&quot;\n    set &quot;__VERSION=1.23&quot;\n    set &quot;__YEAR=2017&quot;\n\n    set &quot;__BAT_FILE=%~0&quot;\n    set &quot;__BAT_PATH=%~dp0&quot;\n    set &quot;__BAT_NAME=%~nx0&quot;\n\n    set &quot;OptHelp=&quot;\n    set &quot;OptVersion=&quot;\n    set &quot;OptVerbose=&quot;\n\n    set &quot;UnNamedArgument=&quot;\n    set &quot;UnNamedOptionalArg=&quot;\n    set &quot;NamedFlag=&quot;\n\n:parse\n    if &quot;%~1&quot;==&quot;&quot; goto :validate\n\n    if /i &quot;%~1&quot;==&quot;/?&quot;         call :header &amp; call :usage &quot;%~2&quot; &amp; goto :end\n    if /i &quot;%~1&quot;==&quot;-?&quot;         call :header &amp; call :usage &quot;%~2&quot; &amp; goto :end\n    if /i &quot;%~1&quot;==&quot;--help&quot;     call :header &amp; call :usage &quot;%~2&quot; &amp; goto :end\n\n    if /i &quot;%~1&quot;==&quot;/v&quot;         call :version      &amp; goto :end\n    if /i &quot;%~1&quot;==&quot;-v&quot;         call :version      &amp; goto :end\n    if /i &quot;%~1&quot;==&quot;--version&quot;  call :version full &amp; goto :end\n\n    if /i &quot;%~1&quot;==&quot;/e&quot;         set &quot;OptVerbose=yes&quot;  &amp; shift &amp; goto :parse\n    if /i &quot;%~1&quot;==&quot;-e&quot;         set &quot;OptVerbose=yes&quot;  &amp; shift &amp; goto :parse\n    if /i &quot;%~1&quot;==&quot;--verbose&quot;  set &quot;OptVerbose=yes&quot;  &amp; shift &amp; goto :parse\n\n    if /i &quot;%~1&quot;==&quot;--flag&quot;     set &quot;NamedFlag=%~2&quot;   &amp; shift &amp; shift &amp; goto :parse\n    if /i &quot;%~1&quot;==&quot;-f&quot;         set &quot;NamedFlag=%~2&quot;   &amp; shift &amp; shift &amp; goto :parse\n\n    if not defined UnNamedArgument     set &quot;UnNamedArgument=%~1&quot;     &amp; shift &amp; goto :parse\n    if not defined UnNamedOptionalArg  set &quot;UnNamedOptionalArg=%~1&quot;  &amp; shift &amp; goto :parse\n\n    shift\n    goto :parse\n\n:validate\n    if not defined UnNamedArgument call :missing_argument &amp; goto :end\n\n:main\n    if defined OptVerbose (\n        echo **** DEBUG IS ON\n    )\n\n    echo UnNamedArgument:    &quot;%UnNamedArgument%&quot;\n\n    if defined UnNamedOptionalArg      echo UnNamedOptionalArg: &quot;%UnNamedOptionalArg%&quot;\n    if not defined UnNamedOptionalArg  echo UnNamedOptionalArg: not provided\n\n    if defined NamedFlag               echo NamedFlag:          &quot;%NamedFlag%&quot;\n    if not defined NamedFlag           echo NamedFlag:          not provided\n\n:end\n    call :cleanup\n    exit /B\n\n:cleanup\n    REM The cleanup function is only really necessary if you\n    REM are _not_ using SETLOCAL.\n    set &quot;__NAME=&quot;\n    set &quot;__VERSION=&quot;\n    set &quot;__YEAR=&quot;\n\n    set &quot;__BAT_FILE=&quot;\n    set &quot;__BAT_PATH=&quot;\n    set &quot;__BAT_NAME=&quot;\n\n    set &quot;OptHelp=&quot;\n    set &quot;OptVersion=&quot;\n    set &quot;OptVerbose=&quot;\n\n    set &quot;UnNamedArgument=&quot;\n    set &quot;UnNamedArgument2=&quot;\n    set &quot;NamedFlag=&quot;\n\n    goto :eof\n</code></pre>\n"}, {'answer_id': 50653047, 'author': 'Garret Wilson', 'author_id': 421049, 'author_profile': 'https://Stackoverflow.com/users/421049', 'pm_score': 4, 'selected': False, 'text': '<p>Inspired by an <a href="https://stackoverflow.com/a/14298769/421049">answer elsewhere</a> by @Jon, I have crafted a more general algorithm for extracting named parameters, optional values, and switches.</p>\n\n<p>Let us say that we want to implement a utility <code>foobar</code>. It requires an initial command. It has an optional parameter <code>--foo</code> which takes an <em>optional</em> value (which cannot be another parameter, of course); if the value is missing it defaults to <code>default</code>. It also has an optional parameter <code>--bar</code> which takes a <em>required</em> value. Lastly it can take a flag <code>--baz</code> with no value allowed. Oh, and these parameters can come in any order.</p>\n\n<p>In other words, it looks like this:</p>\n\n<pre><code>foobar &lt;command&gt; [--foo [&lt;fooval&gt;]] [--bar &lt;barval&gt;] [--baz]\n</code></pre>\n\n<p>Here is a solution:</p>\n\n<pre><code>@ECHO OFF\nSETLOCAL\nREM FooBar parameter demo\nREM By Garret Wilson\n\nSET CMD=%~1\n\nIF "%CMD%" == "" (\n  GOTO usage\n)\nSET FOO=\nSET DEFAULT_FOO=default\nSET BAR=\nSET BAZ=\n\nSHIFT\n:args\nSET PARAM=%~1\nSET ARG=%~2\nIF "%PARAM%" == "--foo" (\n  SHIFT\n  IF NOT "%ARG%" == "" (\n    IF NOT "%ARG:~0,2%" == "--" (\n      SET FOO=%ARG%\n      SHIFT\n    ) ELSE (\n      SET FOO=%DEFAULT_FOO%\n    )\n  ) ELSE (\n    SET FOO=%DEFAULT_FOO%\n  )\n) ELSE IF "%PARAM%" == "--bar" (\n  SHIFT\n  IF NOT "%ARG%" == "" (\n    SET BAR=%ARG%\n    SHIFT\n  ) ELSE (\n    ECHO Missing bar value. 1&gt;&amp;2\n    ECHO:\n    GOTO usage\n  )\n) ELSE IF "%PARAM%" == "--baz" (\n  SHIFT\n  SET BAZ=true\n) ELSE IF "%PARAM%" == "" (\n  GOTO endargs\n) ELSE (\n  ECHO Unrecognized option %1. 1&gt;&amp;2\n  ECHO:\n  GOTO usage\n)\nGOTO args\n:endargs\n\nECHO Command: %CMD%\nIF NOT "%FOO%" == "" (\n  ECHO Foo: %FOO%\n)\nIF NOT "%BAR%" == "" (\n  ECHO Bar: %BAR%\n)\nIF "%BAZ%" == "true" (\n  ECHO Baz\n)\n\nREM TODO do something with FOO, BAR, and/or BAZ\nGOTO :eof\n\n:usage\nECHO FooBar\nECHO Usage: foobar ^&lt;command^&gt; [--foo [^&lt;fooval^&gt;]] [--bar ^&lt;barval^&gt;] [--baz]\nEXIT /B 1\n</code></pre>\n\n<ul>\n<li>Use <code>SETLOCAL</code> so that the variables don\'t escape into the calling environment.</li>\n<li>Don\'t forget to initialize the variables <code>SET FOO=</code>, etc. in case someone defined them in the calling environment.</li>\n<li>Use <code>%~1</code> to remove quotes.</li>\n<li>Use <code>IF "%ARG%" == ""</code> and not <code>IF [%ARG%] == []</code> because <code>[</code> and <code>]</code> don\'t play will at all with values ending in a space.</li>\n<li>Even if you <code>SHIFT</code> inside an <code>IF</code> block, the current args such as <code>%~1</code> don\'t get updated because they are determined when the <code>IF</code> is parsed. You could use <code>%~1</code> and <code>%~2</code> inside the <code>IF</code> block, but it would be confusing because you had a <code>SHIFT</code>. You could put the <code>SHIFT</code> at the end of the block for clarity, but that might get lost and/or confuse people as well. So "capturing" <code>%~1</code> and <code>%~1</code> outside the block seems best.</li>\n<li>You don\'t want to use a parameter in place of another parameter\'s optional value, so you have to check <code>IF NOT "%ARG:~0,2%" == "--"</code>.</li>\n<li>Be careful only to <code>SHIFT</code> when you <em>use</em> one of the parameters.</li>\n<li>The duplicate code <code>SET FOO=%DEFAULT_FOO%</code> is regrettable, but the alternative would be to add an <code>IF "%FOO%" == "" SET FOO=%DEFAULT_FOO%</code> outside the <code>IF NOT "%ARG%" == ""</code> block. However because this is still inside the <code>IF "%PARAM%" == "--foo"</code> block, the <code>%FOO%</code> value would have been evaluated and set before you ever entered the block, so you would never detect that <em>both</em> the <code>--foo</code> parameter was present <em>and also</em> that the <code>%FOO%</code> value was missing.</li>\n<li>Note that <code>ECHO Missing bar value. 1&gt;&amp;2</code> sends the error message to stderr.</li>\n<li>Want a blank line in a Windows batch file? You gotta use <code>ECHO:</code> or one of the variations.</li>\n</ul>\n'}, {'answer_id': 51156281, 'author': 'Amol Patil', 'author_id': 1811749, 'author_profile': 'https://Stackoverflow.com/users/1811749', 'pm_score': 2, 'selected': False, 'text': '<p>Simple solution(even though question is old)</p>\n\n<p>Test1.bat</p>\n\n<pre><code>echo off\necho "Batch started"\nset arg1=%1\necho "arg1 is %arg1%"\necho on\npause\n</code></pre>\n\n<p>CallTest1.bat</p>\n\n<pre><code>call "C:\\Temp\\Test1.bat" pass123\n</code></pre>\n\n<p>output</p>\n\n<pre><code>YourLocalPath&gt;call "C:\\Temp\\test.bat" pass123\n\nYourLocalPath&gt;echo off\n"Batch started"\n"arg1 is pass123"\n\nYourLocalPath&gt;pause\nPress any key to continue . . .\n</code></pre>\n\n<p>Where YourLocalPath is current directory path. </p>\n\n<p>To keep things simple store the command param in variable and use variable for comparison. </p>\n\n<p>Its not just simple to write but its simple to maintain as well so if later some other person or you read your script after long period of time, it will be easy to understand and maintain.</p>\n\n<p>To write code inline : see other answers.</p>\n'}, {'answer_id': 54635244, 'author': 'Io-oI', 'author_id': 8177207, 'author_profile': 'https://Stackoverflow.com/users/8177207', 'pm_score': 3, 'selected': False, 'text': '\n<p><a href="https://i.stack.imgur.com/L2hkd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L2hkd.png" alt="enter image description here" /></a></p>\n<blockquote>\n<p><em>For to use looping get all arguments and in pure batch:</em></p>\n</blockquote>\n<p><strong>Obs:</strong> For using without: <strong><code>?*&amp;|&lt;&gt;</code></strong></p>\n<hr />\n<pre class="lang-bat prettyprint-override"><code>@echo off &amp;&amp; setlocal EnableDelayedExpansion\n\n for %%Z in (%*)do set &quot;_arg_=%%Z&quot; &amp;&amp; set/a &quot;_cnt+=1+0&quot; &amp;&amp; (\n     call set &quot;_arg_[!_cnt!]=!_arg_!&quot; &amp;&amp; for /l %%l in (!_cnt! 1 !_cnt!\n     )do echo/ The argument n:%%l is: !_arg_[%%l]!\n )\n\ngoto :eof \n</code></pre>\n<blockquote>\n<p><em>Your code is ready to do something with the argument number where it needs, like...</em></p>\n</blockquote>\n\n<pre class="lang-bat prettyprint-override"><code> @echo off &amp;&amp; setlocal EnableDelayedExpansion\n\n for %%Z in (%*)do set &quot;_arg_=%%Z&quot; &amp;&amp; set/a &quot;_cnt+=1+0&quot; &amp;&amp; call set &quot;_arg_[!_cnt!]=!_arg_!&quot;\n \n fake-command /u !_arg_[1]! /p !_arg_[2]! &gt; test-log.txt\n \n</code></pre>\n'}, {'answer_id': 58447844, 'author': 'The Godfather', 'author_id': 2261656, 'author_profile': 'https://Stackoverflow.com/users/2261656', 'pm_score': 5, 'selected': False, 'text': '<p>In batch file</p>\n<pre><code>set argument1=%1\nset argument2=%2\necho %argument1%\necho %argument2%\n</code></pre>\n<p><code>%1</code> and <code>%2</code> return the first and second argument values respectively.</p>\n<p>And in command line, pass the argument</p>\n<pre><code>Directory&gt; batchFileName admin P@55w0rd \n</code></pre>\n<p>Output will be</p>\n<pre><code>admin\nP@55w0rd\n</code></pre>\n'}, {'answer_id': 64749745, 'author': 'npocmaka', 'author_id': 388389, 'author_profile': 'https://Stackoverflow.com/users/388389', 'pm_score': 2, 'selected': False, 'text': '<h2>Paired arguments</h2>\n<p>If you prefer passing the arguments in a key-value pair you can use something like this:</p>\n<pre><code>@echo off\n\nsetlocal enableDelayedExpansion\n\n:::::  asigning arguments as a key-value pairs:::::::::::::\nset counter=0\nfor %%# in (%*) do (    \n    set /a counter=counter+1\n    set /a even=counter%%2\n    \n    if !even! == 0 (\n        echo setting !prev! to %%#\n        set &quot;!prev!=%%~#&quot;\n    )\n    set &quot;prev=%%~#&quot;\n)\n::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n:: showing the assignments\necho %one% %two% %three% %four% %five%\n\nendlocal\n</code></pre>\n<p>And an example :</p>\n<pre><code>c:&gt;argumentsDemo.bat one 1 &quot;two&quot; 2 three 3 four 4 &quot;five&quot; 5\n1 2 3 4 5\n</code></pre>\n<h2>Predefined variables</h2>\n<p>You can also set some environment variables in advance. It can be done by setting them in the console or <a href="https://www.computerhope.com/issues/ch000549.htm" rel="nofollow noreferrer">setting them from my computer</a>:</p>\n<pre><code>@echo off\n\nif defined variable1 (\n    echo %variable1%\n)\n\nif defined variable2 (\n    echo %variable2%\n)\n</code></pre>\n<p>and calling it like:</p>\n<pre><code>c:\\&gt;set variable1=1\n\nc:\\&gt;set variable2=2\n\nc:\\&gt;argumentsTest.bat\n1\n2\n</code></pre>\n<h2>File with listed values</h2>\n<p>You can also point to a file where the needed values are preset.\nIf this is the script:</p>\n<pre><code>@echo off\n\nsetlocal\n::::::::::\nset &quot;VALUES_FILE=E:\\scripts\\values.txt&quot;\n:::::::::::\n\n\nfor /f &quot;usebackq eol=: tokens=* delims=&quot; %%# in (&quot;%VALUES_FILE%&quot;) do set &quot;%%#&quot;\n\necho %key1% %key2% %some_other_key%\n\nendlocal\n</code></pre>\n<p>and values file is this:</p>\n<pre><code>:::: use EOL=: in the FOR loop to use it as a comment\n\nkey1=value1\n\nkey2=value2\n\n:::: do not left spaces arround the =\n:::: or at the begining of the line\n\nsome_other_key=something else\n\nand_one_more=more\n</code></pre>\n<p>the output of calling it will be:</p>\n<blockquote>\n<p>value1 value2 something else</p>\n</blockquote>\n<p>Of course you can combine all approaches. Check also <a href="https://ss64.com/nt/syntax-args.html" rel="nofollow noreferrer">arguments syntax</a> , <a href="https://ss64.com/nt/shift.html" rel="nofollow noreferrer">shift</a></p>\n'}, {'answer_id': 67860255, 'author': 'i Mr Oli i', 'author_id': 11199164, 'author_profile': 'https://Stackoverflow.com/users/11199164', 'pm_score': 5, 'selected': False, 'text': '<p>Everyone has answered with really complex responses, however it is actually really simple. <code>%1 %2 %3</code> and so on are the arguements parsed to the file. <code>%1</code> is arguement 1, <code>%2</code> is arguement 2 and so on.</p>\n<p>So, if I have a bat script containing this:</p>\n<pre class="lang-sh prettyprint-override"><code>@echo off\necho %1\n</code></pre>\n<p>and when I run the batch script, I type in this:</p>\n<pre><code>C:&gt; script.bat Hello\n</code></pre>\n<p>The script will simply output this:</p>\n<pre><code>Hello\n</code></pre>\n<p>This can be very useful for certain variables in a script, such as a name and age. So, if I have a script like this:</p>\n<pre class="lang-sh prettyprint-override"><code>@echo off\necho Your name is: %1\necho Your age is: %2\n</code></pre>\n<p>When I type in this:</p>\n<pre><code>C:&gt; script.bat Oliver 1000\n</code></pre>\n<p>I get the output of this:</p>\n<pre><code>Your name is: Oliver\nYour age is: 1000\n</code></pre>\n'}, {'answer_id': 69130861, 'author': 'Zimba', 'author_id': 5958708, 'author_profile': 'https://Stackoverflow.com/users/5958708', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re worried about security/password theft (that led you to design this solution that takes login credentials at execution instead of static hard coding without the need for a database), then you could store the api or half the code of password decryption or decryption key in the program file, so at run time, user would type username/password in console to be hashed/decrypted before passed to program code for execution via <code>set /p</code>, if you\'re looking at user entering credentials at run time.</p>\n<p>If you\'re running a script to run your program with various user/password, then command line args will suit you.</p>\n<p>If you\'re making a test file to see the output/effects of different logins, then you could store all the logins in an encrypted file, to be passed as arg to test.cmd, unless you wanna sit at command line &amp; type all the logins until finished.</p>\n<p>The number of args that can be supplied is <a href="https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/command-line-string-limitation" rel="nofollow noreferrer">limited to total characters on command line</a>. To overcome this limitation, the previous paragraph trick is a workaround without risking exposure of user passwords.</p>\n'}]
['https://Stackoverflow.com/questions/26551', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/730/']