我有一只带标签的蝙蝠,因此我启动蝙蝠检查是否传递了任何参数,如果没有,则执行将对蝙蝠本身进行新的调用并传递一个参数,完成后它将继续执行下一个命令。
当 bat 通过接受参数的调用启动时,它的执行会在 :label 内循环(永久)
它是一个需要很长时间才能运行的蝙蝠,因此调用会导致窗口标题中出现“无限”进度条,并保持在后台循环执行。
因为看到一条静止的线很乏味(“海底总动员、档案等……”) 无论是在屏幕上还是在标题栏中,最近一个需要很长时间的任务/脚本的示例是通过扫描所有驱动程序并比较结果来找到一个较新的可执行文件(版本/日期),以获得一个(较新的),回答这里。
以上面链接的耗时任务示例代码为例,并在标题中添加进度条,我有以下代码:
@echo off
if "%~1."=="." (call start ".:." /b cmd.exe /v:on /c "%~f0" ? )else goto %:^|
setlocal enabledelayedexpansion && set "_f=" && set "_ff=" && set /a "_cnt=0"
for /f usebackq^tokens^=* %%f in (`%__AppDir__%mountvol.exe^|find.exe /i ":\"
`)do 2>nul pushd %%~f && (set/a "_cnt+=1" && call %:^) _ff_!_cnt! & popd)
for /f useback^tokens^=1*delims^=^=^ %%i in (`^<nul ^<con: set _ff_ ^|%__AppDir__%sort.exe /r
`)do <con: <nul call set "_ffprobe=%%~dpnxj" & call set "_ffprob_Path=%%~dpj" & goto %:^V
%:^)
for /f usebackq^tokens^=2^,4* %%i in =;(`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^|^
%__AppDir__%sort.exe /r^|^|exit/b`)do set "_f=%%~k" && for /f useback^delims^=. %%G in (`
^<con %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^|^
%__AppDir__%findstr.exe /rb [0-9]`)do <con: <nul call set "%~1=%%~k" && exit /b 0
%:^|
>nul chcp 65001 & mode 89,5 & set "_title=Looking for your latest ffprob.exe, please wait^!!"
<con: ;color 0a && set^ "_176=░░░░░░░░░░" && set^ "_255=▓▓▓▓▓▓▓▓▓▓" && set^ "_022=▬▬▬▬▬▬▬▬▬▬"
call setlocal enabledelayedexpansion & set "_ping=%__AppDir__%pathping.exe 127.1 -q 1 -p 050"
%:^/
set "_title=I'm doing something that takes a while, please wait^!!"
for %%G in =;(1,2,3,4,5,6,0)do if "!_ffprob_Path!\." == "\." =;( if ".\%%~G\." == ".\1\." =;(
for /l %%L in (0 01 10)do %_ping% |title !_title! [!_255:~0,%%~L!!_022:~%%L!] !time:~,8!
)else if "%%G\" == "2\" =;(if "!_ffprob_Path!\." == "\." =;(
for /l %%L in (10 -1 0)do %_ping% |title !_title! [!_255:~0,%%~L!!_176:~%%L!] !time:~,8!
))else if "%%G\" == "3\" =;(if "!_ffprob_Path!\." == "\." =;(
for /l %%L in (0 01 10)do %_ping% |title !_title! [!_022:~0,%%~L!!_176:~%%L!] !time:~,8!
))else if "%%G\" == "4\" =;(if "!_ffprob_Path!\." == "\." =;(
for /l %%L in (10 -1 0)do %_ping% |title !_title! [!_022:~0,%%~L!!_255:~%%L!] !time:~,8!
))else if "%%G\" == "5\" =;(if "!_ffprob_Path!\." == "\." =;(
for /l %%L in (0 01 10)do %_ping% |title !_title! [!_176:~0,%%~L!!_255:~%%L!] !time:~,8!
))else if "%%G\" == "6\" =;(if "!_ffprob_Path!\." == "\." =;(
for /l %%L in (10 -1 1)do %_ping% |title !_title! [!_176:~0,%%~L!!_022:~%%L!] !time:~,8!
))else if "%%G\" == "0\" =;(if "!_ffprob_Path!\." == "\." =;(
goto %:^/
)));=
%:^V
rem./ your code continues at this point using: / & echo+!_ffprob_Path! & echo+!_ffprobe!
结果是“无限”进度条启动后不会停止,即使在设置了变量之后也是如此,它应该在进度条标签中发出循环结束信号,但它不起作用......
尝试过多次启用/禁用 setlocal、反转标签调用的顺序以及我甚至不记得的事情,但尝试过几次结束循环但都没有成功......
如何结束同一bat后台运行的标签的循环?
我已经在这个点上停留了很长时间,却什么也没看到。
任何想法?
常规布局中的相同代码......
@echo off
if "%~1." == "." (
call start ".:." /b cmd.exe /v:on /c "%~f0" ?
) else (
goto %:^p
)
setlocal enabledelayedexpansion
set "_f="
set "_ff="
set /a "_cnt=0"
for /f "usebackq tokens=*" %%f in (`%__AppDir__%mountvol.exe ^| %__AppDir__%find.exe /i ":\"`) do 2>nul (
pushd %%~f && (
set /a "_cnt+=1"
call %:^) _ff_!_cnt!
popd
)
)
for /f "usebackq tokens=1* delims==" %%i in (`set _ff_ ^| %__AppDir__%sort.exe /r`) do (
call set "_ffprobe=%%~dpnxj"
call set "_ffprob_Path=%%~dpj"
goto %:^V
)
%:^)
for /f "usebackq tokens=2,4*" %%i in (`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^| %__AppDir__%sort.exe /r ^|^| exit /b`) do (
set "_f=%%~k"
for /f "usebackq delims=." %%G in (`^<con: call %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^| %__AppDir__%findstr.exe /rb [0-9]`)do (
call set "%~1=%%~k"
exit /b 0
)
)
%:^p
>nul chcp 65001
%__AppDir__%mode.com con: cols=89 lines=5
set "_title=Looking for your latest ffprob.exe, please wait^!!"
color 0a
set "_176=░░░░░░░░░░"
set "_255=▓▓▓▓▓▓▓▓▓▓"
set "_022=▬▬▬▬▬▬▬▬▬▬"
setlocal enabledelayedexpansion
set "_ping=%__AppDir__%pathping.exe 127.1 -q 1 -p 050"
%:^/
for %%G in =;(1,2,3,4,5,6,0)do (
if "!_ffprob_Path!\." == "\." (
if ".\%%~G\." == ".\1\." (
for /l %%L in (0 01 10) do %_ping% | title !_title! [!_255:~0,%%~L!!_022:~%%L!] !time:~,8!
)else if "%%G\" == "2\" (
if "!_ffprob_Path!\." == "\." (
for /l %%L in (10 -1 0) do %_ping% | title !_title! [!_255:~0,%%~L!!_176:~%%L!] !time:~,8!
)
)else if "%%G\" == "3\" (
if "!_ffprob_Path!\." == "\." (
for /l %%L in (0 01 10) do %_ping% | title !_title! [!_022:~0,%%~L!!_176:~%%L!] !time:~,8!
)
)else if "%%G\" == "4\" (
if "!_ffprob_Path!\." == "\." (
for /l %%L in (10 -1 0) do %_ping% | title !_title! [!_022:~0,%%~L!!_255:~%%L!] !time:~,8!
)
)else if "%%G\" == "5\" (
if "!_ffprob_Path!\." == "\." (
for /l %%L in (0 01 10) do %_ping% | title !_title! [!_176:~0,%%~L!!_255:~%%L!] !time:~,8!
)
)else if "%%G\" == "6\" (
if "!_ffprob_Path!\." == "\." (
for /l %%L in (10 -1 1) do %_ping% | title !_title! [!_176:~0,%%~L!!_022:~%%L!] !time:~,8!
)
)else if "%%G\" == "0\" (
if "!_ffprob_Path!\." == "\." (
goto %:^/
)
)
)
)
%:^V
rem./ your code continues at this point using: /
echo+!_ffprob_Path!
echo+!_ffprobe!
答案1
我的方法是在作为单独线程启动的标签中使用无限 for /l 循环。我使用文件的 ADS 从主文件向线程传达何时停止,然后使用硬退出退出线程。
:# Author: T3RRY Created: 03/06/2021
:# Purpose: RGB Spinner animation for load / waiting indication while script continues to run.
:# Utilises a second thread to display animation while the main script continues executing.
:# Execution and termination of the thread is controlled using a value stored in an alternate data
:# stream of this file:. 1 = run ; 0 = stop
@Echo off & CD /D "%~dp0"
:# thread label structure by Jeb - https://stackoverflow.com/a/68636825/12343998
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
Call :defMacros
%= Quit if not NTFS =% If Errorlevel 1 Exit /b
Cls
===:# Usage Examples.
%Start.Thread:MSG=Example 1%
For /l %%i in (1 1 100000)Do Set "var=%%i"
%End.Thread%
Echo([%var%] iterations actioned.
%Start.Thread:MSG=Example 2%
For /l %%i in (1 1 10000)Do Set "var=%%i"
%End.Thread%
Echo([%var%] iterations actioned.
%Start.Thread:MSG=Example 3%
Set "var=1"
%End.Thread%
Echo([%var%] iterations actioned.
:# End script cleanup
Powershell -c "remove-item -path '%~nx0' -Stream '*'"
CHCP %active.cp% > nul
@Pause
Goto :Eof
===========================================================
:AnimThread - Animation; RGB or BW spinner
:# Animation sequence. Symbols will not display correctly with certain fonts
:# Recommended font: Lucida Console
:# Alt 205 ═ 186 ║ 187 ╗ 200 ╚ 188 ╝ 201 ╔
:# ╔═╗╔═╗╔═╗╔═╗
:# ║/║║-║║\║║|║
:# ╚═╝╚═╝╚═╝╚═╝
:#
:# Framerate: ~ 25 fps. ('Delay' assigned as 4/100ths of a second.)
:# NOTE - Multiple instances of this thread can not be run in parallel;
:# as Virtual terminal Cursor save operation will overide information stored by parallel threads
Setlocal EnableDelayedExpansion
CHCP 65001 > nul
If "%_Param%"=="MSG" Set "_Param=Executing"
If defined Win10 <nul set /p "=%\E%[1E%_Param%.%\E%[?25l%\E%[1E%\E%7"
Set "title.i=-1"
Set "T2[L]=0" %= Rem Spinner Loop count =%
Set "T2[chars]= |/-\" %= Rem Spinner characters =%
Set "Delay=4" %= Framerate in Centiseconds =%
:# frame control via elapsed time by Dave Benham - https://www.dostips.com/forum/viewtopic.php?f=3&t=4741
If defined Win10 For /l %%. in ()Do (
%= Check completion status flagged in status stream of this file; =%
(For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do if %%G EQU 0 (
<nul Set /P "=%\E%[?25h%\E%8%\E[K%" %= restore cursor =%
%restore.CP% > nul %= restore codepage =%
<nul Set /P "=%\E%[7;32mCompleted.%\E%[0m%\E%[1E%\E%[M%\E%[K" %= notify completion; Emit newline; Delete last line of animation =%
(TITLE )
Exit %= exit thread =%
))2> nul
%= Calculate time elapsed =%
for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100
if !tDiff! geq !delay! (
Set /A title.i+=1
If !title.i! GTR 9 Set "title.i=0"
Set "Bar=║0123456789║"
For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
Title Processing... !BAR! !Time!
%= delay has expired ; display frame =%
Set /A "T2[L]+=1" %= Increment index point of spinner string =%
%= Output Spinner frame. Overwites previous output using saved cursor position =%
For /f "Delims=" %%v in ("!T2[L]!")Do <nul Set /p "=%\E%8%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╔═╗╔═╗╔═╗%\E%[0;38;2;150;;150m%\E%[K%\E%[E║%\E%[38;2;20;20;20;7m%\E%[48;2;255;;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;255;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;100;255m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║%\E%[0m%\E%[K%\E%[E%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╚═╝╚═╝╚═╝%\E%[0m%\E%[K%\E%[1G%\E%[2A%\E%7"
If !T2[L]! EQU 4 Set "T2[L]=0" %= Reset index point of spinner string =%
set /a t1=t2
)
)
%= If not defined Win10 [ No Virtual Terminal support ] =%
For /l %%. in ()Do (
(For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do if %%G EQU 0 (
%restore.CP% > nul
Cls
Echo(Complete.
(TITLE )
Exit
))2> nul
for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100
if !tDiff! geq !delay! (
Set /A title.i+=1
If !title.i! GTR 9 Set "title.i=0"
Set "Bar=║0123456789║"
For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
Title Processing... !BAR! !Time!
Set /A "T2[L]+=1"
For /f "Delims=" %%v in ("!T2[L]!")Do (
Cls
Echo(%_Param%.
Echo(╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗
Echo(║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║
Echo(╚═╝╚═╝╚═╝╚═╝╚═╝╚═╝
)
If !T2[L]! EQU 4 Set "T2[L]=0"
set /a t1=t2
)
)
:defMacros
:# OS Requirement tests
:# Verify NTFS drive
(Echo(verify.NTFS >"%~f0:Status") || (
Echo(This file must be located on an NTFS drive as it utilises alternate data streams.
Timeout /t 5
Exit /B 1
)
:# Windows Version control. Assigns flag true if system is windows 10 build GTR 10586
:# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
:# Version 1511 build number = 10.0.10586
Set "Win10="
For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "Win10=True"
:# If Win10 true ; Test if virtual terminal codes enabled ; enable if false
:# removes win10 flag definition if version does not support Virtual Terminal sequences
If defined Win10 (
Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel REG_DWORD 0x1" > nul || (
Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
) > Nul || Set "Win10="
)
=====:# Store codepage to restore on script completion
For /f "tokens=4 Delims=: " %%1 in ('CHCP')Do Set "active.cp=%%1"
=====:# Define Escape character for Virtual terminal sequences
If Defined Win10 For /f "Delims=" %%e in ('Echo(Prompt $E^|Cmd')Do Set "\E=%%e"
==================:# macros start and stop Thread via t/f flag written to alternate data stream.
:# Start.Thread - Save current Codepage;
:# Remove status thread from this file if present;
:# Notify command has started;
:# Flag status 1 in Stream :status;
:# Use start /b with params to relaunch this script and execute second thread
:# executes infinite loop in AnimThread1 based on win10 virtual terminal support
Set "Start.Thread=Set "_param=MSG"& (For /F "tokens=2 Delims=:" %%G in ('CHCP')Do Set "restore.CP=CHCP %%G")&((Echo(1)>"%~f0:Stream") & Start /AboveNormal /b "" "%~d0\:AnimThread:\..%~pnx0""
:# End.Thread - Flag status 0 in Stream :Status;
:# Execute a small delay to ensure thread has time to enable cursor display and clear animation.
:# [Delay of approx. 1/66th of a second]
Set "End.Thread=((Echo(0)>"%~f0:Stream")&For /L %%i in (1 1 750)Do ((Call ))"
Exit /b 0