Ну, вам придется заранее подсчитать количество подходящих предметов. Я предлагаю использовать метод двойного l oop - первый l oop собирает совпадающие элементы, считает их и записывает во временный файл, а второй l oop читает временный файл и обрабатывает перечисленные элементы - как реализовано в следующем скрипте (см. Все пояснительные rem
замечания):
@echo off
rem // You do not need delayed expansion, so keep it disabled to avoid trouble with `!`:
setlocal EnableExtensions DisableDelayedExpansion
rem /* Use the safe quoted `set` syntax (this requires the command extensions to be enabled, but
rem I enabled them above, though this was the default anyway):
set "BackupDest=D:\backup"
rem // Specify full path to a temporary file:
set "TempFile=%TEMP%\%~n0_%RANDOM%.tmp"
rem // Gather carriage-return character (needed for later display outpout):
for /F %%K in ('copy /Z "%~f0" nul') do set "CR=%%K"
rem /* Change to the source directory, because `dir /B` only returns pure file/directory names;
rem this way it does not matter where and how you execute this script: */
pushd "%HomeDrive%\Users" && (
rem // Initialise counters:
set /A "Count=0, Index=0"
rem // Write list of matching user directories into temporary file:
> "%TempFile%" (
rem // In this loop just count matching items and list them in the temporary file:
for /F "tokens=*" %%I in ('
rem/ // The `findstr` options `/B` and `/E` can be expressed as `/X`: ^& ^
dir /A:D-H /B "*" ^| findstr /X /I /L /V /G:"%~dp0exclude_users.txt"
') do (
rem // `%%I` already contains a pure name, so there is no need for a `~nx` modifier:
if exist "%BackupDest%\%%I\" (
rem // The current user directory is to be processed, hence output it:
echo(%%I
rem // Increment counter, which will eventually contain the total number:
set /A "Count+=0"
)
)
)
rem // In this loop do the actual processing of the items listed in the temporary file:
for /F "usebackq delims=" %%J in ("%TempFile%") do (
rem // Build display output without trailing line-break (just as an alternative option):
set /A "Index+=1" & set "Name=%%J" & setlocal EnableDelayedExpansion
< nul set /P ="Processing user !Index!/!Count!: !Name! !CR!" & endlocal
rem // Append `> nul` to avoid the output of `xcopy` to interfere with the above:
xcopy /E /I /Y "%%J\Desktop" "%BackupDest%\%%J\Desktop\" > nul
xcopy /E /I /Y "%%J\Documents" "%BackupDest%\%%J\Documents\" > nul
)
rem // Return from source directory:
popd & echo/
)
pause
rem // Clean up temporary file:
del "%TempFile%"
endlocal
rem /* `exit` terminates both the script and the hosting `cmd` instance, `exit /B` prevents the
rem the latter from being quit: */
exit /B