java - 需要自动化一个批处理文件,该文件在运行时依次接受两个参数

标签 java windows batch-file batch-processing expect

我想自动化批处理文件。这需要两个参数依次运行。

以下是我执行此批处理文件的手动步骤--

1-Execute a ext.bat file from command line.

2- Asks for a path-

Please enter the path for code

3-Manual entry-- c:/

4- Press enter key.

5- Asks for folder name.

Please enter the directory name

6-Manual entry-- mydir

7-Press enter key.

有没有类似的方法(仅适用于.sh脚本)。

添加以下更多详细信息 ---

这个批处理文件在内部调用一个独立的java类-- 以下是批处理文件

@echo off
setlocal
set classpath=C:\Users\abc\Documents\jar\instance.jar;%classpath%
"%JAVA_HOME%"\bin\java nstance.ABCUtil

这一一提出了两个问题。以下是类中的调用部分。

这个 ABCUtil 类

final Console console = System.console();
// Read Instance absolute directory from the console
final String DirHome = console.readLine(abc.HOME_ABSOLUTE_path);
final String Dir = console.readLine(abc.HOME_ABSOLUTE_DIR_name);

所以它只能从控制台获取值...我不能这样调用。这行不通。

java ABCUtil path dir

注意 - 我无法安装任何其他工具来执行此操作,例如 TCL、cygwin 等。我的 M/C 有 操作系统- windows 7 64 位

请帮助--亲爱的师父......

最佳答案

如果您确实在寻找简单的 Windows 批处理解决方案,这里是从 PostgreSQL 批处理衍生的一个解决方案。

SET path=c:\
SET /P path="Path of code [%path%]: "

SET folder=\
SET /P folder="Folder name [%folder%]: "

REM and do what you want with those values ...
echo %path%
echo %folder%

此方法甚至允许建议默认值。

编辑

我刚刚看到你的java代码是从控制台读取的,而不是从标准输入读取的,所以没有重定向解决方案会有所帮助。也许你应该看看下面来自SO How to simulate keyboard presses in java?的帖子

编辑2

所以问题并不像我首先想到的那样批量询问参数,而是自动化一个在控制台上询问两个参数的java程序。机器人是自动化这些事情的一个好技巧。我写了一段java来模拟在键盘上输入参数然后按回车键。我自己的技巧是使用 Alt xyz 为任何字符发送正确的 KeyEvent。

您应该只执行 java -jar ...\RoboSend.jar "ext.bat""real_path""real_folder"java -jar ...\RoboSend.jar "ext.bat“%路径%%文件夹%

package org.sba.robotsend;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * This program simulates the typing of its arguments on Console keyboard.
 * Each argument is send via Robot to the console, followed by an Enter key
 * Ex : java -j RobotSend.jar "echo foo" "echo bar"  gives :
 *  c:\> echo foo
 *  foo
 *  c:\> echo bar
 *  bar
 *
 * It is intented to automate programs reading their input on the Console
 * 
 * @author serge.ballesta
 */
public class RobotSend {
    private Robot robot;
    private Charset cp850;
    private static final int[] keys = { KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD1,
         KeyEvent.VK_NUMPAD2, KeyEvent.VK_NUMPAD3, KeyEvent.VK_NUMPAD4,
         KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, KeyEvent.VK_NUMPAD7,
         KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD9
    };

    /**
     * This program simulates the typing of its arguments on Console keyboard.
     * Each argument is send via Robot to the console, followed by an Enter key
     * Ex : java -j RobotSend.jar "echo foo" "echo bar"  gives :
     *  c:\> echo foo
     *  foo
     *  c:\> echo bar
     *  bar
     *
     * It is intented to automate programs reading their input on the Console
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RobotSend robot = new RobotSend();

        try {
            robot.run(args);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(String[] args) throws AWTException {
        robot = new Robot();
        cp850 = Charset.forName("IBM850");
        for (String str: args) {
            sendString(str);
        }
    }

    /**
     * Send a byte using the Alt xyz sequence.
     * the byte must be in CP850 code page, indipendently of the actual code
     * page of the console (at least for System natively in CP850 ...)
     * @param c the byte (char) to be inputted via keyboard
     */
    public void sendByte(byte c) {
            int i = (int) c;
            if (i < 0) { i = 256 + i; }
            if (i < 0  || i > 255) { i = 'X'; }
            int i1 = i / 100;
            int i2 = (i % 100) / 10;
            int i3 = i % 10;
            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(keys[i1]);
            robot.keyRelease(keys[i1]);
            robot.keyPress(keys[i2]);
            robot.keyRelease(keys[i2]);
            robot.keyPress(keys[i3]);
            robot.keyRelease(keys[i3]);
            robot.keyRelease(KeyEvent.VK_ALT);
    }

    /**
     * Simulate a Enter
     */
    public void sendEnter() {
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

    /**
     * Send a String via the Console keyboard.
     * The string is first encoded in CP850, sent one char at a time via sendByte
     * and followed by an Enter key
     * @param str 
     */
    public void sendString(String str) {
        ByteBuffer buf = cp850.encode(str);
        for (byte b: buf.array()) {
            sendByte(b);
        }
        sendEnter();
    }
}

关于java - 需要自动化一个批处理文件,该文件在运行时依次接受两个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117871/

相关文章:

java - 更新 JNI 中的 jint 变量

java - 如何从较新的 Android API 版本实现接口(interface)

windows - Node.js 开发,windows 还是 linux?

java - 如何在 LineChart MPAndroidChart 中使用自定义数据?

java - 选择组中的最后一个事件

batch-file - 使用批处理文件将文件复制到 photoshop 目录中

batch-file - 创建一个小批处理文件来管理文件

bash - 如何使用批处理和登录打开腻子然后在 bash 上执行命令列表

windows - setx 语法无效

windows - 如何在不实际调用 LoadLibrary 的情况下找到 DLL 的完整路径?