我正在尝试修复一些简单但似乎无法解决的问题。
我有这个非常简单的批处理文件,当你将文件夹放在上面时,它会将一堆文件转换为另一种格式。我试图在标题栏上添加一个进度指示器,但出于某种原因,我无法让它set /a
添加一个值并返回除我最初输入的值之外的任何内容。以下是代码
set Count=0
set NumFiles=0
set oldpct=-1
for %%A in (*.t64) do set /A NumFiles += 1
echo File count = %NumFiles%
echo.
echo Converting...
rem Parse every file in the folder and perform the magic.
for %%x in (*.t64) do (
set /A "Count += 1"
echo count: %Count%
pause
set /A pct=100*%Count%/%NumFiles%
%C1541_PATH%\c1541.exe -verbose off -silent on -format "fromt64,01" d64 "%%~nx.d64" -tape "%%x"
rem calculate progress
if "%pct%" gtr "%oldPct%" (
set oldPct=%pct%
echo %pct%
title Progress: %pct%/100
)
)
pause
exit
:F_Error
cls
echo FILE ERROR
echo.
echo This script only works on folders, not individual files.
pause
exit
:Error
cls
echo ---------------------- ERROR! ----------------------
echo.
echo Please drag and drop a folder on this batch file!
echo Please make sure C1541.exe is in the
echo correct folder and that the path value in
echo this file points to it.
echo Look near the top of the script, it is identified.
echo.
pause
exit
为了保持简短,我故意省略了开始和结束的部分代码。
由于某种原因,%count%
和%pct%
拒绝被计算......
当我尝试使用 delayedexpansion 时,我收到此消息
但由于它是一个不依赖于 WOW64 或 wmic 的脚本,所以我使用的位大小并不重要(命令提示符会使所有内容变得平滑)
答案1
我终于破解了它!
看来您无法在 FOR IN 循环中可靠地执行 SET /A 函数。但是,如果您创建一个单独的子例程,并在 FOR IN 循环中调用它,那么它就可以工作。
剧本的主要部分是这样的
for %%A in (*.t64) do set /A NumFiles += 1
echo File count = %NumFiles%
echo.
echo Converting...
rem Parse every file in the folder and perform the magic.
for %%x in (*.t64) do (
call :progress
%C1541_PATH%\c1541.exe -verbose off -silent on -format "fromt64,01" d64 "%%~nx.d64" -tape "%%x"
)
exit
:progress
set /A Count += 1
set /A pct=100 * Count / NumFiles
title Progress: %pct%/100
exit /b
不需要像我想象的那样 DELAYEDEXPANSION 或 SETLOCAL。
答案2
Windows 10 64 位
还有另一种使用 cmd 显示/跟踪进度的方法。
ping -n 2 8.8.8.8 >NUL
用你的命令替换
使用两个 for 循环、延迟扩展、回显、标题和重定向显示/跟踪进度。
我更想知道一个命令需要多长时间才能完成。
如果你不知道如何在 for 循环中使用延迟扩展,请阅读robvanderwoude.com
开始CMD:
制作文件.txt:
1.t64
2.t64
3.t64
4.t64
5.t64
从 file.txt 创建测试文件:
for /f %a in (file.txt) do cd.> %a
追踪与显示
cmd /v /e /q
cd.> %temp%\track.txt
for %a in (*.t64) do set /a NumFiles +=1
for /l %a in (1,1,%NumFiles%) Do (
title Start command #%a of %NumFiles% at !time!
echo Start command #%a of %NumFiles% at !time!
rem Replace ping -n 2 8.8.8.8 >NUL w/ your command
ping -n 2 8.8.8.8 >NUL
echo End command #%a of %NumFiles% at !time!
)>> %temp%\track.txt
exit
结束命令:
开始脚本:
从 files.txt 创建测试文件:
for /f %%a in (file.txt) do cd.> %%a
追踪与显示
@rem Track / Display progress with cmd
@rem Windows 10 64-bit
@echo off
setlocal enableextensions enabledelayedexpansion
cd.> %temp%\track.txt
for %%a in (*.t64) do set /a NumFiles +=1
for /l %%a in (1,1,%NumFiles%) Do (
title Start command #%%a of %NumFiles% at !time!
echo Start command #%%a of %NumFiles% at !time!
rem Replace ping -n 2 8.8.8.8>NUL with your command
ping -n 2 8.8.8.8>NUL
echo End command #%%a of %NumFiles% at !time!
)>> %temp%\track.txt
exit /b
结束脚本:
开始当 for 循环运行时,计算循环已完成的百分比,并使用 cmd 标题显示该百分比
如何在 cmd for 循环中正确实现延迟扩展并消除调用、子程序和变量。
命令:
cmd /e /v /q
for %a in (*.t64) do set /a NumFiles +=1
for /l %a in (1,1,%NumFiles%) do (
set /a pct=100 * %a / %NumFiles%
title Progress: Running command #%a of %NumFiles% at !time! !pct!% of %Numfiles% commands
rem Replace ping -n 2 8.8.8.8>NUL w/ your command
ping -n 2 8.8.8.8> NUL
)
exit
脚本:
@rem As a for loop is running calculate what percent of the loop has completed and display the percentage with cmd title
@rem Windows 10 64-bit
@echo off
setlocal enableextensions enabledelayedexpansion
for %%a in (*.t64) do set /a NumFiles +=1
for /l %%a in (1,1,%NumFiles%) do (
set /a pct=100 * %%a / %NumFiles%
rem title requires %% to display % in script
title Progress: Running command #%%a of %NumFiles% at !time! !pct!%% of %Numfiles% commands
rem Replace ping -n 2 8.8.8.8 >NUL w/ your command
ping -n 2 8.8.8.8>NUL
)
exit /b
结束 当 for 循环运行时,计算循环已完成的百分比,并使用 cmd 标题显示该百分比