[英]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.