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.
Here\'s what the command line looks like:
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 call
ed 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
-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
-
before parameters.