7

I added a DOSKEY alias via batch script (script1.bat) and try to call it in another batch script. It doesn't work.

script1.bat:

set USER_SETTINGS=%DRIVE%\programme\settings.xml
DOSKEY mvn=mvn --settings %USER_SETTINGS% -X $*

script2.bat:

mvn clean install

When I call mvn clean install from the console, it works. The debug output is forthcoming. When I call script2.bat from the same console, no debug output is coming.

Can anyone help?

emi-le
  • 756
  • 9
  • 26
  • 5
    Batch files do not use interactive input, which is what `DOSKEY` works on. This cannot work, as far as I can tell. – Joey Apr 14 '16 at 07:15
  • 1
    Hi Joey, you could have posted an answer. It's one even if it is not the outcome I hoped for.... I found another explanation [here](http://unix.stackexchange.com/questions/1496/why-doesnt-my-bash-script-recognize-aliases). The concept is the same. And the explanation, that Aliases may not be the same on different systems is a logical explanation why it shouldn't be done. Even though this is true for environment variables also. – emi-le Apr 14 '16 at 08:17

2 Answers2

4

If you show the doskey help via doskey /? you get something like: "Recall and edit commands at the DOS prompt, and create macros". A Batch file is not the DOS prompt: the DOSKEY command works with keys pressed as input, like arrows or F7 keys.

For this reason, the next code should work:

script2.bat:

@if (@CodeSection == @Batch) @then


@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem Send the keys with the DOSKEY macro name:
%SendKeys% "mvn clean install{ENTER}"

goto :EOF


@end


// JScript section


WshShell.SendKeys(WScript.CreateObject("WScript.Shell").Arguments(0));

Further details at Press Keyboard keys using a batch file

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    Hi Aacini, thanks for your answer. In my case this is not really applicable, because I have many different versions of mvn calls in many different scripts. In my case the version with setting the settings in `MAVEN_OPTS` was the better solution. But it's interesting that it's possible to imitate keypressing via batch file. – emi-le Apr 14 '16 at 13:17
  • The explanation is good, but the workaround is brittle and therefore ill-advised. – mklement0 Apr 27 '17 at 03:12
  • @mklement0: This solution works perfectly. The method is not practical because DOSKEY alias was designed to be used via the keyboard, not via a Batch file. However, this solution does exactly what the OP requested: call a DOSKEY alias via a batch script, isn't it? – Aacini Apr 27 '17 at 04:05
  • Part of what makes an answer a good answer is pointing out an OP's misguided solution attempt and suggesting a better approach. Instead, your answer perfects the misguided attempt - kudos for virtuosity in doing so, but it's still a misguided solution. – mklement0 Apr 27 '17 at 04:09
  • @mklement0: Well, "brittle" is not the same that "misguided", right? (I wonder what will be your next adjective **;)** My solution works and show what is the problem with the original approach. I invite you to indicate what would be the "better approach" you are talking about in this case. However, if it is "This not works, don't use it", then that would be a worst answer than the mine! – Aacini Apr 27 '17 at 14:33
  • My original comment stated (emphasis added) "is brittle and _therefore_ ill-advised". "Misguided", which I later used, is probably preferable to "ill-advised", because it better illustrates that the approach is fundamentally the wrong one. Either way: The approach is misguided/ill-advised, _because_ it is inherently brittle and limited to interactive invocation scenarios. In other words: the brittleness implies the misguidedness. – mklement0 Apr 27 '17 at 14:48
  • As for a better approach: use `call :label …` subroutines or variations of batch files to encapsulate predefined arguments. – mklement0 Apr 27 '17 at 14:55
-2
  • doskey DOES work in batch files

  • Maybe your doskey string does not work

For example, try running this file, junk.bat :

doskey m=echo hi
cmd /k "echo try typing m now"
Jason
  • 1,974
  • 24
  • 19
  • 1
    The OP is not looking for _interactive_ use. The idea was to use the DOSKEY definitions as aliases for _commands in a batch file_ (which isn't supported). (On a side note: a DOSKEY definition made in a batch file also takes global effect in the console window.) – mklement0 Apr 27 '17 at 03:11
  • 2
    The question is about "DOSKEY **alias**", not about the `doskey` command itself. Although you may run `doskkey` command in a Batch file (for example, to _define_ an alias or macro), the use of a doskey **ALIAS** or macro does _not_ use the `doskey` command and does NOT work in Batch files... – Aacini Apr 27 '17 at 03:54