每移动 1000 个文件更新一次微调器

Spinner with update for every 1000 files moved

我有很久以前在某个地方找到的微调器的以下代码。我试图弄清楚如何修改它,以便它显示每移动 1000 个文件的更新。所以,它看起来像这样:

 Moving XML Files...|  1,000 Files moved
 Moving XML Files.../  2,000 Files moved
 Moving XML Files...-  3,000 Files moved
 Moving XML Files...\  4,000 Files moved

微调器字符继续移动的地方。我将 运行 处理近一百万个文件,因此我确实需要指示状态。非常感谢任何更好方法的帮助或建议。

代码

@echo off
setlocal

Call :SpinnerEx
exit /b


:SpinnerEx
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

FOR /L %%n in (1,1,50) DO (
    call :spinner
    ping localhost -n 1 > nul
)
exit /b

:spinner
set /a "spinner=(spinner + 1) %% 4"
set "spinChars=\|/-"
<nul set /p ".=Moving XML Files...!spinChars:~%spinner%,1!!CR!"
exit /b

是Magoo提供的实际执行移动的代码

基于 Magoo 的脚本,我将替换

) DO SET "filename=%%a"&CALL :process

) DO (
    SET "filename=%%a"&CALL :process
    rem increment file counter
    rem if total divided by 1000 has no remainder, advance the spinner
)

像这样:

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "spinChars=\|/-"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
SET "filesmoved=0"

PUSHD "%sourcedir%"

set /P "=Moving XML Files...%spinChars:~0,1%  0 Files moved"<NUL

FOR /f "tokens=1*delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\*_*_*.xml" '
 ) DO (
    SET "filename=%%a"&CALL :process
    set /a filesmoved += 1, thousand = filesmoved %% 1000
    setlocal enabledelayedexpansion
    if !thousand! equ 0 call :spinner
    endlocal
)

POPD

GOTO :EOF

:process
FOR /f "tokens=2,3,6delims=_" %%m IN ("%filename%") DO SET "date1=%%m"&SET "date2=%%n"&SET "whichdate=%%o"
IF DEFINED whichdate SET "date1=%date2%"
IF NOT DEFINED date2 GOTO :eof
ECHO(MD .\%date1:~0,4%\%date1:~4,2%
ECHO(MOVE "%filename%" .\%date1:~0,4%\%date1:~4,2%\
GOTO :EOF

:spinner
set "moved=%filesmoved%"
:spinner2
if %filesmoved% geq 4000 set /a filesmoved -= 4000 & goto :spinner2
set /a spinpos = filesmoved / 1000
for /L %%I in (1,1,50) do set /P "=%BS%"<NUL
set /P "=Moving XML Files...!spinChars:~%spinPos%,1!  %moved% Files moved"<NUL
goto :EOF

for /f... ("prompt $H...") 行将退格字符捕获到变量(到 %BS%)。 for /L %%I in (1,1,50) 行退格 50 次。希望其余部分是不言自明的。

如果您想在不实际移动任何文件的情况下测试逻辑,这里是用简单的 for /L 循环替换文件迭代循环的相同脚本:

@ECHO OFF
SETLOCAL
SET "spinChars=\|/-"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
SET "filesmoved=0"

set /P "=Moving XML Files...%spinChars:~0,1%  0 Files moved"<NUL

for /L %%I in (1,1,50000) do (
    set /a filesmoved += 1, thousand = filesmoved %% 1000
    setlocal enabledelayedexpansion
    if !thousand! equ 0 call :spinner
    endlocal    
)

goto :EOF

:spinner
set "moved=%filesmoved%"
:spinner2
if %filesmoved% geq 4000 set /a filesmoved -= 4000 & goto :spinner2
set /a spinpos = filesmoved / 1000
for /L %%I in (1,1,50) do set /P "=%BS%"<NUL
set /P "=Moving XML Files...!spinChars:~%spinPos%,1!  %moved% Files moved"<NUL
goto :EOF