Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

batch file - Set /p in a loop?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...