Set /p in a loop?

2019-01-15 17:37发布

This is probably impossible, but I have a loop that displays a animated logo by using TYPE to type logo_(framenumber).txt and the framenumber is determined by a loop:

:s
if %m%==379 set m=0
cls
TYPE Logo_%m%.txt
set /a m=%m%+1
goto s

I wanted to be able to use a set /p option and without disturbing/stopping the loop so the animation plays while a user is typing in the set /p input. I think there is a way to do it with FOR but I'm not sure how. Any ideas? Thanks.

2条回答
成全新的幸福
2楼-- · 2019-01-15 17:55

Although this topic is somewhat old, I just discovered it. This is a pure Batch file solution that works pretty well:

EDIT: I slightly modified the code in order to made it simpler.

@echo off
setlocal EnableDelayedExpansion
if "%1" equ "Animate" goto %1

for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
cd . > input.txt
start "" /B "%~F0" Animate

set "input="
:nextKey
   set "key="
   for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%K"
   if "!key:~-1!" equ "!CR!" goto endInput
   if "!key:~-1!" equ "!BS!" (
      if defined input set "input=%input:~0,-1%"
   ) else (
      set "input=%input%!key:~-1!"
   )
   set /P "=%input%" > input.txt < NUL
goto nextKey
:endInput
del input.txt
echo/
echo/
echo Input read: "%input%"
goto :EOF


:Animate
set "banner=                              Enter your name please                              "
set m=0
:loop
   if not exist input.txt exit
   set /A m=(m+1)%%51
   cls
   echo/
   echo/     !banner:~%m%,31!
   echo/
   echo/
   if exist input.txt (type input.txt) else exit
   ping -n 1 -w 300 localhost > NUL
   ping -n 1 -w 300 localhost > NUL
   ping -n 1 -w 300 localhost > NUL
goto loop

In this solution the animated "logo" is replaced by a banner, but the method to display a series of files is practically the same.

查看更多
祖国的老花朵
3楼-- · 2019-01-15 18:00

EDIT: This is possible in batch. See Aacini's answer.

This is not possible with batch files. Batch commands are single-threaded. To run two things simultaneously requires two instances of cmd.exe. But the console subsystem only allows one program to own the console at a time, so if the second instance of cmd is attached to the same console, one of them must be blocked.

It is possible to do something like this with a win32 executable which uses WriteConsoleOutput to modify the characters on the console screen. If you do that, you are no longer limited to just dumping text files, but the downside is that it's a lot more work than calling type in batch.

查看更多
登录 后发表回答