Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在命令行上自动执行仅接受交互式参数的可执行文件(执行时不能指定参数)_Windows_Batch File_Command Line - Fatal编程技术网

Windows 在命令行上自动执行仅接受交互式参数的可执行文件(执行时不能指定参数)

Windows 在命令行上自动执行仅接受交互式参数的可执行文件(执行时不能指定参数),windows,batch-file,command-line,Windows,Batch File,Command Line,我有一个可执行文件,可以从命令行交互运行。下面是它的外观: C:\Users\Me> my_executable.exe # Running the executable from CMD Welcome! Please choose one: 0: Exit 1: Sub-task 1 2: Sub-task 2 Enter your input: 2 # I entered this interactively Sub-task 2 chosen. Ple

我有一个可执行文件,可以从命令行交互运行。下面是它的外观:

C:\Users\Me> my_executable.exe  # Running the executable from CMD

Welcome! Please choose one:
0: Exit
1: Sub-task 1
2: Sub-task 2
Enter your input: 2             # I entered this interactively

Sub-task 2 chosen.
Please choose next option:
0: Return to previous menu
1: Connect to server
2: Disconnect from server
3: Call server API 1
4: Call server API 2
Enter your input: 1             # I entered this interactively
我无法在使用标志之前指定输入参数。例如,这类方法都不起作用:

C:\Users\Me> my_executable.exe 2 # Running the executable from CMD with first argument specified

Sub-task 2 chosen.
Please choose next option:
0: Return to previous menu
1: Connect to server
2: Disconnect from server
3: Call server API 1
4: Call server API 2

Enter your input: 

使用批处理文件实现自动化的正确方法是什么?我遇到了一个类似的要求,但不同的是,那里的可执行文件采用命令行参数(与我的情况不同)。

假设您的可执行文件读取stdin,并且不直接访问键盘,那么您可以使用重定向或管道来提供完成运行所需的所有响应

让我们假设您想要指定的2,1响应,但在服务器连接完成后,exe将返回到第一个菜单。假设您想退出,您还需要继续0

要使用重定向,您需要准备一个包含所有所需响应的文本文件,每行一个响应

@echo off
> response.txt (
  echo 2
  echo 1
  echo 0
)
my_executable.exe < response.txt
del response.txt
或者使用FOR循环

@echo off
(for %%A in (2 1 0) do echo %%A) > response.txt
my_executable.exe < response.txt
del response.txt
@echo off
(for %%A in (2 1 0) do echo %%A) | my_executable

如果您对编程或脚本编写感兴趣,可以使用Python或其他自动化工具(如AutoIt)编写简单的代码。我认为基于批处理文件的解决方案应该可以用于此工作流。如果它实际上是批处理文件,则可以使用
%1
%2
等。因此,在脚本中,如果“%1 eq”2“做点什么cmd后面的第一个参数将是
%1
下一个
%2
等等。如果它实际上是一个可执行文件,那么它应该有实际的开关选项。因此,请尝试使用
/?
--help
运行该文件。创建一个inputFile.txt,其中包含两行:
2
1
,然后尝试:
my_executable.exe
。如果这不起作用,请参阅;在这种情况下,先运行SendKeys,然后运行.exe来澄清第一句话,没有控制台应用程序直接访问键盘。问题在于,应用程序可能会调用
ReadConsole
或打开“CONIN$”直接从连接的控制台的输入缓冲区读取,而不是通过
ReadFile
一般读取
StandardInput
句柄。只有窗口管理器(win32k.sys)的内核端可以直接访问键盘驱动程序。它创建键盘输入消息(例如,
WM_KEYDOWN
),这些消息被发布到拥有给定窗口的线程的消息队列中,例如控制台主机实例conhost.exe的输入线程。控制台的输入缓冲区依次包含从这些GUI窗口消息翻译过来的键盘和鼠标事件的输入记录。@eryksun-感谢您的澄清
@echo off
(for %%A in (2 1 0) do echo %%A) | my_executable