简体   繁体   English

处理来自键盘的Windows命令提示符输入并显示输出,还将其重定向到文件?

[英]Handle Windows command prompt input from keyboard and display output and also redirecting it to a file?

Though I can find few relevant questions such as Displaying Windows command prompt output and redirecting it to a file and How do I echo and send console output to a file in a bat script? 尽管我可以找到一些相关的问题,例如“ 显示Windows命令提示符输出并将其重定向到文件”,以及如何在bat脚本中回显并将控制台输出发送到文件? , I could not fix my problem. ,我无法解决问题。 I am working on Windows XP and running a few scripts using a batch file which uses STDIN and STDOUT handles. 我在Windows XP上工作,并使用使用STDIN和STDOUT句柄的批处理文件运行一些脚本。

Basically, I want to save the log file of command prompt. 基本上,我想保存命令提示符的日志文件。 This is possible with "echo >> log.txt" in a batch file. 可以通过批处理文件中的“ echo >> log.txt”来实现。 However, this is not saving the outputs generated as a result of the script file. 但是,这不会保存脚本文件生成的输出。 So I tried at the cmd prompt itself as D:>file.bat >"dir_path/log.txt". 因此,我在cmd提示符下尝试了D:> file.bat>“ dir_path / log.txt”。 Again, this command saves the STDOUT to log.txt at the specified location. 再次,此命令将STDOUT保存到指定位置的log.txt。 Since I need to get a few user inputs ie to use the STDIN handle, how do I achieve this in addition to saving the outputs of command prompts in separate file too? 由于我需要获取一些用户输入(即使用STDIN句柄),除了将命令提示符的输出也保存在单独的文件中之外,如何实现这一点?

The answer is simple: 答案很简单:

    @echo off
    set /p answ1=Get user input1
    set /p answ2=Get User input2
    echo %answ1% and %answ2%>>log.txt
    notepad log.txt
    set /p "var=<log.txt"
    type log.txt

Even if you redirect your .bat file to a file, you can output to the screen with >con . 即使将.bat文件重定向到文件,也可以使用>con输出到屏幕。

Execute the following script with test.bat >test.out . 使用test.bat >test.out执行以下脚本。 It will prompt you for an input on the screen. 它将提示您在屏幕上输入。

timeout -t 2
>con set /p "var1=Prompt1: "
ipconfig
>con set /p "var2=Prompt2: "
echo %var1% %var2%
>con echo %var1% %var2%
exit /b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何随时捕获Windows中命令提示符的显示输出? - How to capture display output from a command prompt in Windows at any moment? 如何在重定向文件中的输出文本时在 Windows 命令行中显示输出文本? - How to display the output text in the Windows command line while redirecting the output text in a file? 如何将Windows命令提示符下发出的命令的Perl脚本输出重定向到文本文件 - How to redirect the Perl script output from command issued in windows command prompt to text file 如何将命令输出保存到Windows中的文件中并在终端上也显示输出? - How to save command output to a file in windows and show output on terminal also? node.js:使用 readline 从文件中获取输入并在 windows 命令提示符中处理 I/O - node.js: take input from file using readline and process I/O in windows command prompt 如何在Windows上通过命令提示符输入对.exe文件的输入? - How to enter input to .exe file through command prompt on Windows? 在MS命令提示符下逐行重定向文件 - Redirecting file line by line in MS command prompt 将文件内容重定向到命令提示符下的变量 - Redirecting contents of a file to a variable in command prompt 使用Ansible从Windows命令提示符输出中提取特定列 - Extract a specific column from the windows command prompt output using Ansible 需要帮助从 Windows 上的计时器程序严重重定向输入和输出 - Need help badly redirecting input and output from timer program on Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM