4

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.

hiandbye
  • 41
  • 4
  • 1
    So why don't you save the macros to a file and easily load them by running doskey. `doskey /macrofile=mymacros.txt` – Squashman Oct 03 '18 at 13:50
  • @Squashman The filename (e.g. the `packagename\Class.java` in my example) changes with each project, so a static file full of macros doesn't make sense. – hiandbye Oct 03 '18 at 14:20
  • 1
    Did you read the help for the DOSKEY command. You can pass arguments to the macro just like you can with batch files. – Squashman Oct 03 '18 at 14:27
  • @Squashman Well, yeah, that's how I implemented the workaround. Passing the command generated by my batch-file to doskey instead of echoing it out. I guess I didn't make that clear in my question. – hiandbye Oct 03 '18 at 17:33
  • You are hard coding the file names in the macro. I am saying you do not have to hard code the file names in the macro. They can be passed to the macro as arguments. – Squashman Oct 03 '18 at 17:40
  • Ah, I see what you mean, now. Create a macro that takes the filename as an argument and inserts it into the expanded command. That would be a simple solution for a couple of compiling jobs. The problem with Java compilation is that `javac` and `java` requiere two different syntaxes. My batch-script creates the syntax for both from one user input. – hiandbye Oct 03 '18 at 18:22

1 Answers1

3

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));
Aacini
  • 65,180
  • 12
  • 72
  • 108