How to code a spinner for waiting processes in a B

2019-01-10 21:26发布

I would like to show the user with a spinner, that something is done in background but do not know how this works in a batchfile.

Does anyone have a clue?

12条回答
相关推荐>>
2楼-- · 2019-01-10 21:59

This routine examines the output of tasklist for a process you START from cmd.
Pass it the name of the exe as a parameter e.g.
call :spinner calc.exe
It reports
Elapsed: 001 seconds
and increments seconds until the exe process terminates.
The Elapsed 001 seconds message is overwritten each second by ECHO.exe -n \r
which echos a cr without a line feed.
Echo.exe is available at http://www.paulsadowski.com/wsh/cmdprogs.htm

 @echo off
 start calc
 call :spinner calc.exe
 pause

 :spinner       
 SET COUNT=1
 :BEGIN
 set "formattedValue=000000%count%"
 ECHO.exe -n Elapsed: %formattedValue:~-3% seconds
 ECHO.exe -n \r    %= -n (suppress crlf) \r output a cr =%

 SET /A COUNT+=1

 set EXE=%1    %= search output of tasklist for EXE =%
 set tl=tasklist /NH /FI "IMAGENAME eq %EXE%"
 FOR /F %%x IN ('%tl%') DO IF %%x == %EXE% goto FOUND
 set result=0
 goto FIN
 :FOUND
 set result=1
 :FIN
 IF %result% EQU 0 GOTO END

 PING -n 2 127.0.0.1 > nul    %= wait for about 1 second =%

 GOTO BEGIN
 :END
查看更多
乱世女痞
3楼-- · 2019-01-10 22:00

The spinner CAN be done in batch script, you just need some variables:

@echo off

:spinner
set mSpinner=%mSpinner%.
if %mSpinner%'==..............................' set mSpinner=.
cls
echo %mSpinner%

rem Check if the process has finished via WMIC and/or tasklist.

goto spinner


:exit

For the BAT itself to detect a process running/exits. You can do that via the WMI command-line interface or the tasklist command of which I have limited knowledge.

If it were back in the DOS days you could even does that without clearing the screen... short of using some combination of escape characters. I don't know if it's still possible on Vista/XP.

查看更多
我命由我不由天
4楼-- · 2019-01-10 22:04

If I understand your question you want a spinner because some operation you are performing is taking time and you want to show to the user that something is happening, right?

In that case, as far as I know, its not possible with the native commands. (it could be possible if you had a program that showed a spinner while executing the operation that take long time)

And it looks like the echo don't support ansi escape sequences (in the old days you had to have ansi.sys loaded, don't know if that still exists) so you can't use ansi to control the cursor.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-10 22:04

If you mean within a Windows batch script, you can't do it natively. The echo statement used to print to the console will always print a newline, and you can't move the cursor.

It's a bit of a hack, but you can do this with a combination of VBScript and batch script.

This VBScript will print a backspace, then it's argument:

WScript.StdOut.Write(chr(8) & WScript.Arguments(0))

Put this in a file, vbsEcho.vbs, then call this script from your batch script. The following batch script will keep displaying the spinner until you press CTRL-C:

@echo off

:LOOP
cscript //nologo vbsEcho.vbs "\"
cscript //nologo vbsEcho.vbs "|"
cscript //nologo vbsEcho.vbs "/"
cscript //nologo vbsEcho.vbs "-"
goto :LOOP

EDIT: Using some of the ideas from aphoria's answer, this script will start the Windows calculator, and display a spinner until the calculator closes:

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION
SET COUNT=1

START CALC

cscript //nologo vbsEcho.vbs "Calculating: \"
:LOOP
IF !COUNT! EQU 1 cscript //nologo vbsEcho.vbs "|"
IF !COUNT! EQU 2 cscript //nologo vbsEcho.vbs "/"
IF !COUNT! EQU 3 cscript //nologo vbsEcho.vbs "-"
IF !COUNT! EQU 4 (
    cscript //nologo vbsEcho.vbs "\"
    set COUNT=1
) else (
    set /a COUNT+=1
)

pslist CALC >nul 2>&1
if %ERRORLEVEL% EQU 1 goto :end

goto :LOOP

:END
cscript //nologo vbsEcho.vbs ". Done."
查看更多
Ridiculous、
6楼-- · 2019-01-10 22:07

:LOOP ECHOX -n "~r%Processing..." IF %CTR% EQU 4 SET /A CTR=0 IF %CTR%==0 (set /p DOT=³)

查看更多
Emotional °昔
7楼-- · 2019-01-10 22:09

If you don't mind the screen clearing...try this:

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION
SET COUNT=1

START CALC

:BEGIN
  CLS
  IF !COUNT! EQU 1 ECHO \
  IF !COUNT! EQU 2 ECHO -
  IF !COUNT! EQU 3 ECHO /
  IF !COUNT! EQU 4 ECHO -
  IF !COUNT! EQU 4 (
    SET COUNT=1
  ) ELSE (
    SET /A COUNT+=1
  )
  PSLIST CALC >nul 2>&1
  IF %ERRORLEVEL% EQU 1 GOTO END
GOTO BEGIN

:END

EDIT: This sample will start Calculator and then display a "spinner" until you close Calculator. I use pslist to check for the existence of CALC.EXE. The >nul 2>&1 redirects STDOUT and STDERR to nul so nothing from PSLIST will be displayed.

查看更多
登录 后发表回答