hiandbye
hiandbye

Reputation: 41

Can you add entries to the command line history from a batch file?

Compiling and executing source code for any programming language from Windows cmd in one command can be a hassle, because it's usually a big command that you have to type out at least once, afterwards it's accessible in your command history with the Up-Arrow or F7.

For example, a command to compile and execute Java typically looks like this:

cls & javac packagename\Class.java && java packagename.Class

For convenience I created a batch-file that takes user input and echoes out the complete command.
You can then copy that line, paste it and execute once, and then it's in your command history, quickly accessible via the Up-Arrow.

I would like to simplify that last step even further and push the command created by the batch-file directly into your command history, but I can't find a way to do it.
Even with @echo on, none of the commands executed by a batch-file ever enter the user's history.
Doskey's help doesn't mention anything about adding history items.

Can anybody think of a solution?

Workaround

As a workaround I register a macro with doskey inside the batch-file:

doskey jc=cls ^& javac packagename\Class.java ^&^& java packagename.Class

Then, once you've run the batch file, you only need to type jc to execute the whole command.
This is pretty convenient already, so if no solution can be found, I'll keep using this.

Upvotes: 4

Views: 1674

Answers (1)

Aacini
Aacini

Reputation: 67216

You may use a modified version of this method:

@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 to the keyboard buffer the desired line
%SendKeys% "cls & javac packagename\Class.java && java packagename.Class{ENTER}"

rem Read it, so it is inserted in the command-line history
set /P "="

goto :EOF


@end


// JScript section

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

Upvotes: 3

Related Questions