gpt4 book ai didi

for-loop - 进度条 DOS

转载 作者:行者123 更新时间:2023-12-03 21:14:45 27 4
gpt4 key购买 nike

我是dos批处理命令的新手,我想实现如图所示的进度条。首先,您必须初始化过程值并计算它的百分比,您必须使用进度条显示它。

进度条可以按Alt+219和Alt+176

这是我到目前为止所做的。

echo off
cls
SetLocal EnableDelayedExpansion
set processValueString=200
set a/ processValue=200
set a/ percentage=0

echo Process value: %processValueString%

for /l %%a in (1,1, %processValue%) do (
set a/ percentage = %%a / %processValue% * 100
echo Percent: !percentage! %
echo Processing: !a! / %processValue%
)

最佳答案

ANSI 序列作为 Redstone Update 的一部分添加, 所以它只适用于 Windows 10+


使用 ANSI sequences 的组合和 FORFILES 的能力打印ASCII字符

@echo off
^
%=-----------DO NOT REMOVE THIS LINE-----------=%
Y
%= Y to abort when Ctrl-C is pressed =%
%= N to ignore =%
SETLOCAL EnableDelayedExpansion

::Defaults
( set LF=^
%=-----------DO NOT REMOVE THIS LINE-----------=%
)
FOR /F %%C in ('copy /Z "%~f0" nul') do set "CR=%%C"
FOR /F %%E in ('prompt $E ^& ^<nul cmd /k') do set "ESC=%%E"

::SETTINGS
color a
>nul chcp 65001
mode CON: COLS=120 LINES=31

::INITIALIZE
call :init 0xDB] 0xB0 40 200

echo Starting at %time%
<nul set/p=[
>nul 2>nul call :progessBar
echo Finished at %time%

exit /b


:init bar tbd length processvalue
::Custom parameters defined by user
set "bar=%~1"
set "tbd=%~2"
set /a "barlength=%~3"
set /a "processvalue=%~4"

::Default values
if NOT DEFINED bar set "bar=0xDB"
if NOT DEFINED tbd set "tbd=0xB0"

::Set bar & tbd
>hex.tmp <hex.tmp (
FOR %%V in (bar tbd) do (
FORFILES /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!%%V!"
set/p"%%V=" CLEAR VARIABLE
set/p"%%V=" SET VARIABLE
))

del hex.tmp
exit /b


:main
>&3 (
echo(
for /L %%N in (0 1 %barlength%) do echo(!LF!%ESC%[2A%ESC%[%%NC%tbd%
for /L %%N in (0 1 %processvalue%) do (
set/ashowBar=%%N*barlength/processvalue,percentage=%%N*100/processvalue
echo(Percent: !percentage!%%!LF!Processing: %%N / %processvalue%!LF!%ESC%[3A%ESC%[!showBar!C%bar%
)
echo(!LF!
)
exit /b


:progessBar
<"%~f0" call :main

Sources:


编辑:这个答案限制在 1 bar/second,但是 compatible从 7 开始跨越所有版本的 Windows。(在某些 /NOBREAK 开关上不受支持。)

DosTips ,@Aacini 发现 TIMEOUT重定向到 CON 将光标带回家! @jeb还表明可以通过管道 | 消除输出(通常 TIMEOUT 不支持管道,因此使用 SET/P。)但是,移除顶部的倒计时(几乎)是不可能的。

@echo off
====SETLOCAL EnableDelayedExpansion EnableExtensions
cls


::SETTINGS
color a
>nul "%__APPDIR__%CHCP.COM" 65001
"%__APPDIR__%MODE.COM" CON: COLS=120 LINES=31


::INITIALIZE
::Custom parameters defined by user
set "bar_ASCII=0xDB"
set "tbd_ASCII=0xB0"
set/a"#len=40,#totalValue=200,#newlines=1" %====# of newlines to echo before progressBar, >0====%

::DEFAULTS
set "overwrite=x"
FOR /L %%# in (1,1,6) do set "overwrite=!overwrite!!overwrite!"
%= CLEAR VARIABLES =%
set "loaded="
set "remain="
set "progressBar="


::Set LOADED & REMAIN
>hex.tmp <hex.tmp (
"%__APPDIR__%FORFILES.EXE" /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!bar_ASCII!!tbd_ASCII!"
set/p"=" SKIP empty line
set/p"_=" GET 2nd line
)
del hex.tmp
set "loaded=!_:~0,1!"
set "remain=!_:~1!"


::Set $NEWLINES
FOR /L %%L in (2,1,%#newlines%) do set ^"$newlines=!$newlines!^
%====DO NOT REMOVE ME====%
"
echo(
for /L %%N in (0,1,%#len%) do set "progressBar=!progressBar!!loaded!"
for /L %%N in (0,1,%#totalValue%) do (
set/a"percentage=%%N*100/#totalValue"
echo(!$newlines!
echo(Percent: !percentage!%%
echo(Processing: %%N / %#totalValue%
echo(!progressBar:~0,%%N!
>con "%__APPDIR__%TIMEOUT.EXE" /t 1 /nobreak %====Time delay in SECONDS, do not set to 0====%|"%ComSpec%"/Q /C "FOR /F %%C in ('copy /Z "%~f0" nul') do set/p"=_%%C%overwrite%""
)


====ENDLOCAL
exit /b

关于for-loop - 进度条 DOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61554078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com