User892313
User892313

Reputation: 245

bat-script: Open a program and execute a command in that program

I want to create a script that initializes an apache derby database by opening ij and telling ij to run the setup.sql script.

I've managed to open ij, but how can i pass commands to the ij window from the script window? I belive this is possible with here documents in linux, but as far as i understood, that is not possible in windows command prompt.

Edit: I use this line to open ij.

"C:\Program Files\Java\jdk1.8.0_101\db\bin\ij.bat"

And i want this line to be executed inside the window opened by the previous command:

run 'setup.sql'

Upvotes: 0

Views: 3868

Answers (2)

Akshay Chopra
Akshay Chopra

Reputation: 1253

  1. Make a sql file and list all the commands that you want to run from ij.
    For example:
    1. I want to connect to the derby database.
    2. Then I want to run DDL script to create table
    3. Then I want to run DML script to insert data into tables.

So my sql file will look like this:

CONNECT 'jdbc:derby://<my_hostname>:1527/<database_name>;create=true;user=akshay;password=akshay';

RUN 'C:\Users\akshay_chopra\Downloads\db-script\derby\derby_ddl.sql';

RUN 'C:\Users\akshay_chopra\Downloads\db-script\derby\derby_dml.sql';

Let this sql file name be derby_config.sql

  1. Make a batch file to run ij tool and execute commands from derby_config.sql

Now, make a batch file and add the following lines:

@echo off
cls

java -cp D:\derby\db-derby-10.14.2.0-bin\db-derby-10.14.2.0-bin\lib\* org.apache.derby.tools.ij "C:\Users\akshay_chopra\Desktop\derby_config.sql"

cmd /k

The format of above command is

java -cp <derby-installation>\lib\* org.apache.derby.tools.ij "<sql-file-location- created-in-step-1>"

D:\derby\db-derby-10.14.2.0-bin\db-derby-10.14.2.0-bin\lib\* represents the jars where it can find ij java class. These jars can be found in \lib folder. The * in the end represent that it will search all the jar files to find ij class.

  1. Run this batch file
    When you will run this batch file, it will open the ij tool and will execute the commands that you have written in the sql file created in step-1.

Upvotes: 0

geisterfurz007
geisterfurz007

Reputation: 5874

It is dumb but should work... What my idea is that you just place other code in your batch file telling it to actually type it into the window. This is usually done with SendKeys. More on the Keys here.

So this would be the script:

@if (@CodeSection == @Batch) @then
@echo off
start "C:\Program Files\Java\jdk1.8.0_101\db\bin\ij.bat"
set SendKeys=CScript //nologo //E:JScript "%~f0"
timeout /t 3
%SendKeys% "run 'setup.sql'"
%SendKeys% "{ENTER}"
Goto:eof

@end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Looks massive but is rather harmless ;)

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

and

@end

are required to seperate the batch-script from the magic that types keys; the JScript.

@echo off will simply block any output that the batch file would print for every command.
The next line starts your batch-file that itself starts another command-line application.
Setting the variable SendKeys is not 100% required but makes it easier to read and it saves the string to activate the JScript-Section (-> Keystroke simulation) to make it easier to use.
Change the timeout value based on your needs. I do not know how long it takes for ij to start.
The next line will stupidly write the command character for character (no worries... You will see nothing!).
Goto:eof is required to stop the batch-script before reading through the JScript and throwing errors all over.

I am not 100% sure as I do not have tested it but you might need to start your batch-file over the command-line itself like this:

start /min <your batch-file here>

This will start the file minimized to make sure it will type in the correct window.

Feel free to ask questions!

Upvotes: 1

Related Questions