如何code进行微调在一个批处理文件等待的进程? [英] How to code a spinner for waiting processes in a Batch file?

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

问题描述

我想显示一个微调的用户,有些事情是在后台完成,但不知道这是如何工作的批处理文件。

有没有人有线索?


解决方案

这实际上可以很容易地用纯天然的命令完成后,你只需要知道如何使用比较麻烦他们。没有用的,如VBScript外部工具或讨厌的副作用,如清除屏幕是必要的。

什么你要找的是bash的回声-n 命令,该命令没有输出换行符一行的等价物。在XP中批量,这是通过使用设置/ P 如下(询问用户响应并提示)空输入来实现的:

 < NUL(套/ p =垃圾你好)
回声。再次。

将输出字符串Hello一次。中间没有换行。

这伎俩(利用CTRL-H的,退格字符可以在下面的测试脚本,开始(一前一后)10秒的子任务有20秒暂停和一个15中可以看出第二个子任务有10秒超时。

净荷脚本由实际运行脚本创建,其唯一的要求是,它这样做必须做然后删除一个标志文件结束时,使监视功能将能够检测到它的工作。

请记住,在这个脚本的^ H琴弦实际上是CTRL-H字符时,^ |使用两个单独的字符逃脱管道符号。

 关闭@echo:: LOCALISE环境。
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion::指定的目录。您当前的工作目录使用
::创建临时文件tmp下_ *。*
设置wkdir的=%〜DP0%
设置wkdir的=%wkdir的:〜0,-1%::第一关,10秒的任务有20秒短暂停。
德尔%wkdir的%\\ tmp目录_ *。*2 - ; NUL
回声>>中%wkdir的%\\ tmp_payload.cmd平127.0.0.1 -n 11 ^> NUL
回声>>中%wkdir的%\\ tmp_payload.cmd删除%wkdir的%\\ tmp_payload.flg
呼吁:监测%wkdir的%\\ tmp_payload.cmd%wkdir的%\\ tmp_payload.flg20::二传,15秒的任务有10秒超时。
德尔%wkdir的%\\ tmp目录_ *。*2 - ; NUL:
回声>>中%wkdir的%\\ tmp_payload.cmd平127.0.0.1 -n 16 ^> NUL
回声>>中%wkdir的%\\ tmp_payload.cmd删除%wkdir的%\\ tmp_payload.flg
呼吁:监测%wkdir的%\\ tmp_payload.cmd%wkdir的%\\ tmp_payload.flg10转到:决赛:监控
    :: Create标志文件并开始减少有效载荷。
    回声>>%2假人
    启动/分钟的cmd.exe / c%1    ::开始监视。
    :: i是指示(0 = |,1 = / 2 = - ,3 = \\)。
    :: m是超时前左秒数。
    集I = 0
    设定M = 3%
    < NUL(套/ p Z =等待孩子完成:^ |)    ::环路这里等待完成。
    :循环
        ::等待一秒钟。
        平127.0.0.1 -n 2 - ; NUL        ::更新计数器和输出进度指示器。
        集/一个I = I + 1
        集/一个M = M - 1
        如果%I%EQU 4组I = 0
        如果%I%EQU 0℃; NUL(套/ p Z = ^ H ^ |)
        如果%I%EQU 1< NUL(套/ p Z = ^ H /)
        如果%I%EQU 2'; NUL(套/ p Z = ^ H-)
        如果%I%EQU 3'; NUL(套/ p Z = ^ H \\)        ::结束条件,完成或超时。
        如果不存在2%(
            回声。
            回声。完成。
            转到:决赛
        )
        如果%M%LEQ 0(
            回声。
            回声。 ***错误:超时等待的孩子。
            转到:决赛
        )
        转到:循环
:最后
ENDLOCAL

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?

解决方案

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.

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.

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

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.

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.

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%\tmp_*.*" 2>nul
echo >>"%wkdir%\tmp_payload.cmd" ping 127.0.0.1 -n 11 ^>nul
echo >>"%wkdir%\tmp_payload.cmd" del "%wkdir%\tmp_payload.flg"
call :monitor "%wkdir%\tmp_payload.cmd" "%wkdir%\tmp_payload.flg" 20

:: Second pass, 15-second task with 10-second timeout.
del "%wkdir%\tmp_*.*" 2>nul:
echo >>"%wkdir%\tmp_payload.cmd" ping 127.0.0.1 -n 16 ^>nul
echo >>"%wkdir%\tmp_payload.cmd" del "%wkdir%\tmp_payload.flg"
call :monitor "%wkdir%\tmp_payload.cmd" "%wkdir%\tmp_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

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

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