[Q]Speediest way to count files?
Posted: 21 Mar 2015 02:59
To count the files in %SYSTEMROOT% ( C:\Windows ) I tried 2 algorithms. Both took up the nearly equal amount of time: ~30 secs for 200,977 files. So at 1st glance it seems to be a matter of taste which one to use.
My today's question: Is there a way to get it faster?
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set tmp=%TEMP%\dircount.txt
::
:: Algorithm 1
::
echo(
echo algorithm 1
set /a count=0
echo %TIME%
dir "%SYSTEMROOT%\*.*" /b /s /adhsr /a-d > %tmp%
for /f %%a in (%tmp%) do (set /a count+=1)
set tme=%TIME%
echo %count%
echo %tme%
echo(
::
:: Algorithm 2
::
echo(
echo algorithm 2
SET /a count=0
set tmp2=%TEMP%\dircount2.txt
echo %TIME%
dir "%SYSTEMROOT%\*.*" /b /s /adhsr /a-d > %tmp%
findstr /n ^^ %tmp% > %tmp2%
(
for /f "usebackq delims=" %%a in ("%tmp2%") do (
set /a count+=1
)
) > NUL
set tme=%TIME%
echo %count%
echo %tme%
echo(
::
::
pause
del "%tmp%" > NUL 2>&1
del "%tmp2%" > NUL 2>&1
endlocal
exit /b
My today's question: Is there a way to get it faster?