(view source code of rxdir.bat as plain text)
@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"=="<DIR>" (
SET /A Dirs += 1
) ELSE (
SET /A File += 1
SET /A Size += %%A
)
:: Escape redirection characters to prevent error messages
SET Line=%1
SET Line=%Line:<DIR>=^<DIR^>%
:: Remove quotes, which were added to prevent error messages
:: with unescaped redirection characters, and display result
ECHO.%Line:"=%
GOTO:EOF
:: Display help screen
:Syntax
ECHO.
ECHO RXDir.bat, Version 0.50 beta for Windows NT 4 / 2000 / XP / Server 2003
ECHO Use Regular eXpressions in the DIR command.
ECHO.
ECHO Usage: RXDIR ^<file_spec^>
ECHO.
ECHO Where: ^<file_spec^> is a regular expression specifying the files to display
ECHO.
ECHO Example: RXDIR " AUTOEXEC\..{1,2}T$"
ECHO.
ECHO Notes: 1. This batch file uses EGREP to interpret the regular expression.
ECHO EGREP is available in many varieties on many sites.
ECHO This batch file has been tested with GNU grep 2.4.2, available
ECHO at http://unxutils.sourceforge.net/
ECHO 2. File specifications are case insensitive.
ECHO 3. All matching files will be displayed, including hidden files.
ECHO 4. This batch file searches the current directory only.
ECHO 5. The regular expression also matches the date, time and size parts
ECHO of DIR's output; examine the source to understand the limitations.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
page last modified: 2024-04-16; loaded in 0.0069 seconds