@ECHO OFF :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Check command line arguments IF NOT "%~2"=="" GOTO Syntax ECHO.%* | EGREP "[/?]" >NUL && GOTO Syntax :: Check availability of EGREP; choose any combination of :: arguments that will result in return code/errorlevel 0 EGREP --help >NUL 2>&1 IF ERRORLEVEL 1 ( ECHO. ECHO EGREP not found GOTO Syntax ) :: Use local variables to prevent a mess in the environment variables SETLOCAL :: Use the regular expressions' equivalent of *.* if no filespec was specified IF "%~1"=="" (SET Rx=".*") ELSE (SET Rx="%~1") :: Initialize lines counter and display header of DIR command SET Counter=0 FOR /F "tokens=*" %%A IN ('DIR ^| FIND /N /V "File(s)"') DO CALL :Header %%A :: Initialize tracking counters and display files :: and directories matching the regular expression SET File=0 SET Dirs=0 SET Size=0 FOR /F "tokens=*" %%A IN ('DIR /A /-C /N ^| MORE /E +5 ^| FIND /V "(s)" ^| EGREP -i %Rx%') DO CALL :Display "%%~A" :: Add appropriate number of leading spaces to counted values IF %File% LSS 1000000 SET File= %File% IF %File% LSS 100000 SET File= %File% IF %File% LSS 10000 SET File= %File% IF %File% LSS 1000 SET File= %File% IF %File% LSS 100 SET File= %File% IF %File% LSS 10 SET File= %File% IF %Size% LSS 1000000000000 SET Size= %Size% IF %Size% LSS 100000000000 SET Size= %Size% IF %Size% LSS 10000000000 SET Size= %Size% IF %Size% LSS 1000000000 SET Size= %Size% IF %Size% LSS 100000000 SET Size= %Size% IF %Size% LSS 10000000 SET Size= %Size% IF %Size% LSS 1000000 SET Size= %Size% IF %Size% LSS 100000 SET Size= %Size% IF %Size% LSS 10000 SET Size= %Size% IF %Size% LSS 1000 SET Size= %Size% IF %Size% LSS 100 SET Size= %Size% IF %Size% LSS 10 SET Size= %Size% IF %Dirs% LSS 1000000 SET Dirs= %Dirs% IF %Dirs% LSS 100000 SET Dirs= %Dirs% IF %Dirs% LSS 10000 SET Dirs= %Dirs% IF %Dirs% LSS 1000 SET Dirs= %Dirs% IF %Dirs% LSS 100 SET Dirs= %Dirs% IF %Dirs% LSS 10 SET Dirs= %Dirs% SET File= %File% SET Dirs= %Dirs% :: Capture last line of DIR's output and remove directory count FOR /F "tokens=1*" %%A IN ('DIR /-C') DO SET Free=%%B :: Display "footer" for DIR command with corrected values ECHO. ECHO.%File% File^(s^) %Size% bytes ECHO.%Dirs% %Free% :: Done ENDLOCAL GOTO:EOF :: Display only the first 5 lines of the DIR command :Header :: Increment line counter SET /A File += 1 :: Stop after first 5 lines IF %File% GTR 5 GOTO:EOF :: Remove line numbers, which were added to include empty lines FOR /F "tokens=1* delims=[]" %%K IN ('ECHO.%*') DO ECHO.%%~L GOTO:EOF :: Display files and keep track of total number of files, directories and size :Display :: Check if file or directory and update appropriate counters FOR /F "tokens=3" %%A IN ('ECHO.%1') DO IF "%%A"=="