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
|
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:
|
|||||||||
|
|
Another useful tip is to use %* to mean "all". For example,
When you run:
the above batch file will run:
I may have the syntax slightly wrong, but this is the general idea. |
|||||||||||||||||||||
|
|
Here's how I do it.
Here's what the command line looks like:
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. |
|||||||||
|
|
If you want to intelligently handle missing parameters you can do something like:
|
|||
|
|
|
Accessing batch parameters can be simple with %1, %2, ... %9 or also %*, There is no simple way for complex contents like
The lines expands to
And each line fails, as one of the It can be solved with reading from a temporary file a remarked version of the parameter.
The trick is to enable But to be able to redirect the output of the The extra characters Then reading the rem parameter output from the file, but carefully. And to use param1 in a safe way, enable the delayed expansion. |
|||
|
|
|
Yep, and just don't forget to use variables like If you forget the double |
|||||||||||||
|
|
No need to complicate it. It is simply command %1 %2 parameters, for example
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:
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. |
||||
|
|
Note: IF In that case, use |
||||
|
|
This loops over the batch parameters (%*) either they are quoted or not, then echos each parameter. |
||||
|
|
|
Let's keep this simple. Here is the .cmd file.
Here are 3 calls from the command line.
|
||||
|
|
|
I wrote a simple read_params script that can be called as a function (or external For example, given the following command:
Here's the function:
Limitations
Changelog
|
|||||
|
|
To refer to a set variable in command line you would need to use " %a% " so for example:
Note: This works for Windows 7 pro. |
||||
|
|
|
Make a new batch file (example: openclass.bat) and write this line in the file:
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... |
|||||
|
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?