如何使SET命令不等待答案? [英] How to make the SET command not wait for answer?

查看:106
本文介绍了如何使SET命令不等待答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个聊天程序,它的运行效果很好,我唯一的问题是SET/P命令将一直等到您按某些键然后按ENTER键.我的代码遇到了问题:

I've made a chat program and it works great, the only problem I have is that the SET /P command will wait until you press something and then press ENTER. There's my code that I got problems with:

:CHAT
CLS
SET TEXT=
TYPE "%FILESDIR%\Files\CHAT.cht" << I need this line active all the time.
ECHO.
SET /P TEXT=Text: 
IF NOT "%TEXT%"=="" ECHO %USERNAME%: %TEXT% >> "%FILESDIR%\Files\CHAT.cht"
PING LOCALHOST -n 1 > nul
GOTO CHAT

我想一直更新TYPE命令,但是您需要输入一些内容.无论如何,我可以解决这个问题吗? 我真的不想要另一个cmd屏幕

I want to update the TYPE command all the time, but you need to type something. Is there anyway I can get around this? I really don't want another cmd screen

推荐答案

假设您确实实时实时更新了文本.当用户开始输入文本,然后另一个用户发送要同时显示的文本时会发生什么.您只有一个窗口-这样您会混乱不堪(假设您可以弄清楚如何使其在技术上得以实现).

Suppose you did have the text updated continuously, in real time. What happens when the user begins to enter text, and then the other user sends text to be displayed at the same time. You only have one window - so you get a jumbled mess (assuming you could figure out how to make it happen technically).

您需要一个功能完善的聊天程序,而且还有很长的路要走-您还没有完全考虑到问题所在.

You are a long way from having a functional chat program - you haven't completely thought out the issues.

1)您需要两个独立更新的窗口:

1) You need two windows that are updated independently:

  • 用户可以在其中输入文本的输入窗口

  • An input window where the user can enter text

一个对话框窗口,实时显示所有参与者的运行对话框.

A dialog window that displays the running dialog of all participants, in real time.

Windows批处理不支持多个窗口.但是您可以通过为每个用户运行两个批处理来模拟它,一个用于输入,另一个用于对话框输出.单个批处理脚本可用于两个过程.父脚本可以使用START命令启动多个批处理过程.每个批处理过程将获得其自己的控制台窗口.

Windows batch doesn't support multiple windows. But you can emulate it by having two batch processes running for each user, one for input, the other for dialog output. A single batch script can be used for both processes. The parent script can launch multiple batch processes using the START command. Each batch process will get its own console window.

2)一次只能有一个人(进程)写入文本文件.如果两个人同时尝试写作,您会如何看待?一个将成功,而另一个将失败.您需要一种检测故障并自动重试直到成功的方法.我在如何在Windows下共享日志文件?中描述了一种实现此目的的简单方法.

2) Only one person (process) can write to the text file at a time. What do you think happens if two people try to write at the same time? One will succeed, and the other will fail. You need a method to detect failure and automatically try again until success. I describe a simple method to achieve this at How do you have shared log files under Windows?.

3)如果用户按<Enter>而不输入任何内容,则SET/P将仅返回上一个条目.您应该在SET/P语句之前清除文本变量. 编辑-我现在看到OP在我意想不到的地方已经有这个

3) SET /P will simply return the previous entry if the user presses <Enter> without typing anything. You should clear the text variable prior to your SET /P statement. EDIT - I now see the OP already had this in a place I did not expect

4)您不想在每次更新时都从头开始重新输入整个对话框.您只想显示新添加的行.可以将输入重定向到一个无限的FOR/L循环,并且在循环内您可以使用SET/P读取最新的行.如果未附加任何内容,则它将不返回任何内容(假设变量在SET/P之前已清除).如果什么都没收到,您根本不回声.

4) You don't want to retype the entire dialog from the beginning every time there is an update. You only want to dispaly the newly appended lines. It is possible to redirect input to an endless FOR /L loop, and within the loop you can use SET /P to read the most recent line. If nothing has been appended, then it will return nothing (assuming the variable was cleared before SET /P). You simply don't ECHO anything if nothing was received.

这是一个非常粗糙的工作示例,它演示了上述概念.无法退出程序.您必须关闭两个控制台窗口才能退出.

Here is a very crude working example that demonstrates the above concepts. There is no way to exit the program. You will have to close both console windows to quit.

@echo off
setlocal enableDelayedExpansion
set "dialog=dialog.txt"
if "%~1" equ ":input" (
  title Chat Input
  goto :input
)
start "" "%~f0" :input
title Chat Dialog

::Show Dialog
<"%dialog%" (  for /l %%N in () do (
  set "text="
  set /p "text="
  if defined text echo(!text!
))


:input
cls
set "text="
set /p "text=>"
:write
2>nul (
  >>"%dialog%" (
    echo(%username%: !text!
    (call )
  ) || goto :write
)
goto :input


要成为一个真正有用的聊天程序,还有很长的路要走.但这是一个很好的起点.仍然可以添加一些其他内容.


There is still a long way to go before this is a truly useful chat program. But it is a good starting point. Some additional things that could still be added.

5)一种为每个独立聊天启动新对话框文件的方法.

5) A way to start a new dialog file for each independent chat.

6)一种邀请一个或多个用户加入聊天的方式.

6) A way to invite one or more users to join the chat.

7)退出程序的一种干净方法,包括关闭其他控制台窗口.这需要进程间通信.我在SNAKE.BAT游戏中的 http://www.dostips上进行了演示. com/forum/viewtopic.php?t = 4741 .警告-该脚本中有很多高级概念,因此提取相关信息可能是一个挑战;-)

7) A clean way to exit the program, including closing of the additional console window. This requires interprocess communication. I demonstrate this in my SNAKE.BAT game at http://www.dostips.com/forum/viewtopic.php?t=4741. Warning - there are a lot of advanced concepts in that script, so extracting the relevent information might be a challeng ;-)

8)最好让对话框窗口具有可滚动的显示缓冲区.用户可以通过控制台属性控制缓冲区的大小,但是最好以编程方式控制它.本机批处理无法做到这一点,但是我在 CMD:在不依赖于窗口高度的情况下设置缓冲区高度中展示了混合PowerShell/批处理如何做到这一点. >

8) It would be nice to have the dialog window have a scrollable display buffer. The user can control the size of the buffer via the console properties, but it would be nice to control it programmatically. Native batch cannot do this, but I show how hybrid PowerShell/batch can do this at CMD: Set buffer height independently of window height

这篇关于如何使SET命令不等待答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆