AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1675694
Accepted
Io-oI
Io-oI
Asked: 2021-09-12 15:50:38 +0800 CST2021-09-12 15:50:38 +0800 CST 2021-09-12 15:50:38 +0800 CST

如何将变量发送到后台进程,这是一个循环:蝙蝠本身的标签(如果定义了变量,则停止循环)

  • 772

我有一个带有一些标签的蝙蝠,所以我开始我的 bat 检查是否传递了任何参数,如果没有,执行将通过一个参数对 bat 本身进行新调用,一旦完成,它将继续执行下一个命令。

当 bat 通过一个带参数的调用开始时,它的执行将在 :label 内循环(永久)

这是一个可能需要很长时间才能运行的蝙蝠,因此调用会导致“无限”进度条出现在窗口标题中,并在后台循环执行。

在此处输入图像描述

因为在屏幕上或标题栏中看到静态行(“Finding Nemo, Files, etc...”)很乏味,而且最近一个需要很长时间的任务/脚本示例正在寻找更新的通过扫描所有驱动程序并比较结果以获得一个(更新的)可执行文件(版本/日期),在此处回答。

以上面链接的代码为一个耗时的任务示例,并在标题中添加一个进度条,我有以下代码:

@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 进行了几次尝试,颠倒了标签调用的顺序,以及我什至不记得的事情,但是有几次尝试结束循环但没有成功......

如何结束在同一蝙蝠后台运行的标签的循环?

我已经在这一点上很久了,我什么也没看到。

任何想法?


常规布局中的相同代码...

@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!
powershell batch
  • 1 1 个回答
  • 103 Views

1 个回答

  • Voted
  1. Best Answer
    T3RR0R
    2021-09-13T00:55:08+08:002021-09-13T00:55:08+08:00

    我的方法是在作为单独线程启动的标签中使用无限 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
    
    • 2

相关问题

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve