I am creating some common doskey commands I use on a regular basis. I am trying to have multiple variables in the doskey macro and sometimes a variable will have spaces in it. I have tried quoting the variable values but it won't work. The only thing that does work is just adding more vars to the end of the macro string.
What I would like to do is: doskey keycmd=plink -l user -pw password $1 -batch $2 And then execute it as follows: c:> keycmd hostname "dir /p /users"
What doskey sees when executed though is: $1=hostname, $2="dir, and "dir isn't a command and /p and /users is completely dropped.
To get this to work I had to define it this way: doskey keycmd=plink -l user -pw password $1 -batch $2 $3 $4 And execute as follows: c:> keycmd hostname dir /p /users
Is there a way I can group "dir /p /users" into a single $2 variable? For doskey variable parameters you only get 9, $1..$9. Eventually I could run out on complicated commands. I tried single and double quotes to no avail...but single and/or double quotes seem to work everywhere else in Windows cmd command.
Is there a solution to this or am I just trying to do too much with doskey?
This should do it. Tell the last var to be everything else
keycmd=echo plink -l user -pw password $1 -batch $*
While you might have spaces in the last parameter (using $*
as in wild2010's answer), it is generally not possible to include parameters with spaces in a doskey command.
See Using Doskey:
Unfortunately, although a parameter can be a long filename, it cannot include spaces, even if enclosed in inverted commas.
If a filename includes spaces, it is necessary to **use the 8.3 version of the name. I know of no work around to this**.DosKey macros can also use
$*
which is termed a "global replaceable parameter".
This must be the last part of a macro and is replaced by whatever is on the command line that is not the macro name or assigned to numbered parameters.
If the above example was rewritten as:
DOSKEY D2F=DIR $1 $g $*
then long filenames-with-spaces could be used as the final parameter - but the path name used as a substitute for
$1
still could not contain directory names with spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With