如何为批处理文件中的等待进程编写微调器? [英] How to code a spinner for waiting processes in a Batch file?

查看:17
本文介绍了如何为批处理文件中的等待进程编写微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个微调器向用户展示一些事情是在后台完成的,但不知道它在批处理文件中是如何工作的.

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.

有人知道吗?

推荐答案

这实际上可以用纯原生命令很容易地完成,你只需要知道如何使用它们中更棘手的.无需使用 VBScript 等外部工具或清除屏幕等令人讨厌的副作用.

This can actually be done quite easily with pure native commands, you just have to know how to use the more tricky of them. No use of external tools like VBScript or nasty side effects like clearing the screen are necessary.

您正在寻找的是 bash "echo -n" 命令的等效项,该命令输出没有换行符的行.在 XP 批处理中,这是通过使用带有空输入的set/p"(通过提示询问用户响应)来实现的,如下所示:

What you're looking for is the equivalent of the bash "echo -n" command which outputs a line without the newline. In XP batch, this is achieved by using "set /p" (ask user for response with a prompt) with empty input as follows:

<nul (set /p junk=Hello)
echo. again.

将输出字符串Hello again".没有中间的换行符.

will output the string "Hello again." with no intervening newline.

那个技巧(以及 CTRL-H 的使用,退格字符可以在以下测试脚本中看到,该脚本开始(一个接一个)一个 10 秒的子任务,有 20 秒的超时和 15-具有 10 秒超时的第二个子任务.

That trick (and the use of CTRL-H, the backspace character can be seen in the following test script which starts (one after the other) a 10-second sub-task with a 20-second timeout and a 15-second sub-task with a 10-second timeout.

payload 脚本是由实际运行的脚本创建的,它唯一的要求是它完成它必须做的工作,然后在完成后删除一个标志文件,以便监控功能能够检测到它.

The payload script is created by the actual running script and its only requirement is that it do the work it has to do then delete a flag file when finished, so that the monitor function will be able to detect it.

请记住,此脚本中的 ^H 字符串实际上是 CTRL-H 字符,即 ^|是用于对管道符号进行转义的两个单独字符.

Keep in mind that the ^H strings in this script are actually CTRL-H characters, the ^| is two separate characters used to escape the pipe symbol.

@echo off

:: Localise environment.
setlocal enableextensions enabledelayedexpansion

:: Specify directories. Your current working directory is used
:: to create temporary files tmp_*.*
set wkdir=%~dp0%
set wkdir=%wkdir:~0,-1%

:: First pass, 10-second task with 20-second timeout.
del "%wkdir%	mp_*.*" 2>nul
echo >>"%wkdir%	mp_payload.cmd" ping 127.0.0.1 -n 11 ^>nul
echo >>"%wkdir%	mp_payload.cmd" del "%wkdir%	mp_payload.flg"
call :monitor "%wkdir%	mp_payload.cmd" "%wkdir%	mp_payload.flg" 20

:: Second pass, 15-second task with 10-second timeout.
del "%wkdir%	mp_*.*" 2>nul:
echo >>"%wkdir%	mp_payload.cmd" ping 127.0.0.1 -n 16 ^>nul
echo >>"%wkdir%	mp_payload.cmd" del "%wkdir%	mp_payload.flg"
call :monitor "%wkdir%	mp_payload.cmd" "%wkdir%	mp_payload.flg" 10

goto :final

:monitor
    :: Create flag file and start the payload minimized.
    echo >>%2 dummy
    start /min cmd.exe /c "%1"

    :: Start monitoring.
    ::    i is the indicator (0=|,1=/,2=-,3=).
    ::    m is the number of seconds left before timeout.
    set i=0
    set m=%3
    <nul (set /p z=Waiting for child to finish: ^|)

    :: Loop here awaiting completion.
    :loop
        :: Wait one second.
        ping 127.0.0.1 -n 2 >nul

        :: Update counters and output progress indicator.
        set /a "i = i + 1"
        set /a "m = m - 1"
        if %i% equ 4 set i=0
        if %i% equ 0 <nul (set /p z=^H^|)
        if %i% equ 1 <nul (set /p z=^H/)
        if %i% equ 2 <nul (set /p z=^H-)
        if %i% equ 3 <nul (set /p z=^H)

        :: End conditions, complete or timeout.
        if not exist %2 (
            echo.
            echo.   Complete.
            goto :final
        )
        if %m% leq 0 (
            echo.
            echo.   *** ERROR: Timed-out waiting for child.
            goto :final
        )
        goto :loop
:final
endlocal

这篇关于如何为批处理文件中的等待进程编写微调器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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