DOSKEY alias does not work in batch script (Windows 7)
Asked Answered
M

2

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?

Margrettmarguerie answered 14/4, 2016 at 7:14 Comment(2)
Batch files do not use interactive input, which is what DOSKEY works on. This cannot work, as far as I can tell.Raceme
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. 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.Margrettmarguerie
D
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

Dulosis answered 14/4, 2016 at 10:3 Comment(7)
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.Margrettmarguerie
The explanation is good, but the workaround is brittle and therefore ill-advised.Handkerchief
@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?Dulosis
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.Handkerchief
@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!Dulosis
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.Handkerchief
As for a better approach: use call :label … subroutines or variations of batch files to encapsulate predefined arguments.Handkerchief
C
-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"
Cerell answered 27/4, 2017 at 2:43 Comment(2)
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.)Handkerchief
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...Dulosis

© 2022 - 2024 — McMap. All rights reserved.