进度条dos
Progress Bar Dos
我是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 and the ability of 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:
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- ANSI sequences
FORFILES
- Functional CR within batch script
编辑:此答案限制为1个bar/second,但是compatible 从 7. 开始的所有 windows 版本(某些 /NOBREAK
开关不受支持。)
在DosTips, @Aacini discovered TIMEOUT
redirected to CON
brings the cursor home! @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
通过对 HackingAddict1337 的代码进行一些小的改动,可以有一个固定的(可选择的)条形总长度:
@echo off
cls
SETLOCAL EnableDelayedExpansion
set /a "processValue=200"
set "bar=0xDB" Character used by progress bar (SUPPORTS HEX)
set "tbd=0xB0"
set "barLength=40"
( set LF=^
%=-----------DO NOT REMOVE THIS LINE. the LF variable is for future use in a function, it's currently useless-----------=%
)
FOR /F %%B in ('FORFILES /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!bar!"') do set "bar=%%B"
FOR /F %%B in ('FORFILES /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!tbd!"') do set "tbd=%%B"
FOR /F %%B in ('copy /Z "%~f0" nul') do set "CR=%%B"
FOR /F %%B in ('prompt $E ^& ^<nul cmd /k') do set "ESC=%%B"
for /l %%N in (0,1,%barLength%) do set "emptybar=!emptybar! "
echo Starting at %time%!LF!
for /l %%N in (0 1 !barLength!) do echo(!LF!%ESC%[2A%ESC%[%%NC%tbd%
for /L %%N in (0 1 %processValue%) do (
set /a showBar=%%N*barLength/processValue
set /a percentage=%%N*100/processValue
echo(Processing: %%N / %processValue% = !percentage!%%!LF!%ESC%[2A%ESC%[!showBar!C%bar%
ping -4 -n 1 127.0.0.1 >nul
)
echo !LF!Finished at %time%
pause>nul
ENDLOCAL
(适应 HackingAddicts 更快的代码)
约瑟夫的担忧:
你缺少"measurement bar"的样子,这个应该是ALT+176的样子,这就是我认为的这段代码"echo [%emptybar%]".
改成你想要的样子:
您缺少的另一件事是非常简单的百分比,例如:百分比:10%。
也改编:
Starting at 15:58:44,10
██████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Processing: 32 / 200 = 16%
- 进度条棒的速度是可变的,也就是说每一步的速度不是恒定的。
那是因为四舍五入。我们对此无能为力,除了让 processValue
等于 barLength
或整数倍。
这在这里有效(非常快!):
编辑:为"bar with variable steps"
修改的代码
@echo off
SetLocal EnableDelayedExpansion
rem Define the "amounts" of each one of the (7) steps that comprise this job
set "amounts= 10 6 2 9 3 7 4"
set width=60
set "off="
set "on="
for /F "tokens=1,2" %%X in ('FORFILES /M "%~nx0" /C "cmd /c echo 0xDB 0xB0"') do (
for /L %%i in (1,1,%width%) do set "on=!on!%%X" & set "off=!off!%%Y"
)
rem Get CR, BS and TAB ASCII control characters:
for /F %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem String to go up one line in SET /P command
set "LineUp=%TAB%!BS!!BS!!CR!"
cls
echo Percentage of values processed
echo/
echo %TAB%%off%
echo %TAB%Percent: 0%%
echo %TAB%Processing: 0/%processValue%
set /A "processValue=0, summa=0"
for %%a in (%amounts%) do set /A processValue+=%%a
for %%N in (%amounts%) do (
set /A "summa+=%%N, Percent=summa*100/processValue, PercentOn=Percent*width/100
for %%i in (1,1,4) do set /P "=.!LineUp!" < nul
for %%p in (!PercentOn!) do echo %TAB%!on:~0,%%p!
echo %TAB%Percent: !Percent!%%
echo %TAB%Processing: !summa!/%processValue% (current: %%N^)
timeout /T %%N > nul
)
有关 "go up one line" 方法的详细信息,请参阅 Move cursor to any position using just ECHO command
注意:将光标向上移动一行的方法适用于所有 Windows 版本 除了 Windows 10 使用新控制台。您需要在 cmd.exe window 控制台中启用 "Legacy mode" 才能使用此方法...
如果需要,您可以使用此应用程序轻松打印 ascii 代码:
下载 url :
https://mega.nz/file/2AZA3Y5T#y8z1aIKJHPiHmnlkgv-dYmn0J2fozz83uGdXS7umyLE
进度条的字符也很多
语法很简单:
char [ascii code] [ascii code]
例如:
char 186 186 201 186
您还可以添加空字符:
char 186 " " 186
希望对你有帮助
我是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 and the ability of 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:
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- ANSI sequences
FORFILES
- Functional CR within batch script
编辑:此答案限制为1个bar/second,但是compatible 从 7. 开始的所有 windows 版本(某些 /NOBREAK
开关不受支持。)
在DosTips, @Aacini discovered TIMEOUT
redirected to CON
brings the cursor home! @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
通过对 HackingAddict1337 的代码进行一些小的改动,可以有一个固定的(可选择的)条形总长度:
@echo off
cls
SETLOCAL EnableDelayedExpansion
set /a "processValue=200"
set "bar=0xDB" Character used by progress bar (SUPPORTS HEX)
set "tbd=0xB0"
set "barLength=40"
( set LF=^
%=-----------DO NOT REMOVE THIS LINE. the LF variable is for future use in a function, it's currently useless-----------=%
)
FOR /F %%B in ('FORFILES /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!bar!"') do set "bar=%%B"
FOR /F %%B in ('FORFILES /P "%~dp0." /M "%~nx0" /C "cmd /c echo(!tbd!"') do set "tbd=%%B"
FOR /F %%B in ('copy /Z "%~f0" nul') do set "CR=%%B"
FOR /F %%B in ('prompt $E ^& ^<nul cmd /k') do set "ESC=%%B"
for /l %%N in (0,1,%barLength%) do set "emptybar=!emptybar! "
echo Starting at %time%!LF!
for /l %%N in (0 1 !barLength!) do echo(!LF!%ESC%[2A%ESC%[%%NC%tbd%
for /L %%N in (0 1 %processValue%) do (
set /a showBar=%%N*barLength/processValue
set /a percentage=%%N*100/processValue
echo(Processing: %%N / %processValue% = !percentage!%%!LF!%ESC%[2A%ESC%[!showBar!C%bar%
ping -4 -n 1 127.0.0.1 >nul
)
echo !LF!Finished at %time%
pause>nul
ENDLOCAL
(适应 HackingAddicts 更快的代码)
约瑟夫的担忧:
你缺少"measurement bar"的样子,这个应该是ALT+176的样子,这就是我认为的这段代码"echo [%emptybar%]".
改成你想要的样子:您缺少的另一件事是非常简单的百分比,例如:百分比:10%。
也改编:
Starting at 15:58:44,10
██████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Processing: 32 / 200 = 16%
- 进度条棒的速度是可变的,也就是说每一步的速度不是恒定的。
那是因为四舍五入。我们对此无能为力,除了让processValue
等于barLength
或整数倍。
这在这里有效(非常快!):
编辑:为"bar with variable steps"
修改的代码@echo off
SetLocal EnableDelayedExpansion
rem Define the "amounts" of each one of the (7) steps that comprise this job
set "amounts= 10 6 2 9 3 7 4"
set width=60
set "off="
set "on="
for /F "tokens=1,2" %%X in ('FORFILES /M "%~nx0" /C "cmd /c echo 0xDB 0xB0"') do (
for /L %%i in (1,1,%width%) do set "on=!on!%%X" & set "off=!off!%%Y"
)
rem Get CR, BS and TAB ASCII control characters:
for /F %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem String to go up one line in SET /P command
set "LineUp=%TAB%!BS!!BS!!CR!"
cls
echo Percentage of values processed
echo/
echo %TAB%%off%
echo %TAB%Percent: 0%%
echo %TAB%Processing: 0/%processValue%
set /A "processValue=0, summa=0"
for %%a in (%amounts%) do set /A processValue+=%%a
for %%N in (%amounts%) do (
set /A "summa+=%%N, Percent=summa*100/processValue, PercentOn=Percent*width/100
for %%i in (1,1,4) do set /P "=.!LineUp!" < nul
for %%p in (!PercentOn!) do echo %TAB%!on:~0,%%p!
echo %TAB%Percent: !Percent!%%
echo %TAB%Processing: !summa!/%processValue% (current: %%N^)
timeout /T %%N > nul
)
有关 "go up one line" 方法的详细信息,请参阅 Move cursor to any position using just ECHO command
注意:将光标向上移动一行的方法适用于所有 Windows 版本 除了 Windows 10 使用新控制台。您需要在 cmd.exe window 控制台中启用 "Legacy mode" 才能使用此方法...
如果需要,您可以使用此应用程序轻松打印 ascii 代码:
下载 url :
https://mega.nz/file/2AZA3Y5T#y8z1aIKJHPiHmnlkgv-dYmn0J2fozz83uGdXS7umyLE
进度条的字符也很多
语法很简单:
char [ascii code] [ascii code]
例如:
char 186 186 201 186
您还可以添加空字符:
char 186 " " 186
希望对你有帮助