Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rxdir.bat

(view source code of rxdir.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Check command line arguments
  6. IF NOT "%~2"=="" GOTO Syntax
  7. ECHO.%* | EGREP "[/?]" >NUL && GOTO Syntax
  8.  
  9. :: Check availability of EGREP; choose any combination of
  10. :: arguments that will result in return code/errorlevel 0
  11. EGREP --help >NUL 2>&1
  12. IF ERRORLEVEL 1 (
  13. 	ECHO.
  14. 	ECHO EGREP not found
  15. 	GOTO Syntax
  16. )
  17.  
  18. :: Use local variables to prevent a mess in the environment variables
  19. SETLOCAL
  20.  
  21. :: Use the regular expressions' equivalent of *.* if no filespec was specified
  22. IF "%~1"=="" (SET Rx=".*") ELSE (SET Rx="%~1")
  23.  
  24. :: Initialize lines counter and display header of DIR command
  25. SET Counter=0
  26. FOR /F "tokens=*" %%A IN ('DIR ^| FIND /N /V "File(s)"') DO CALL :Header %%A
  27.  
  28. :: Initialize tracking counters and display files
  29. :: and directories matching the regular expression
  30. SET File=0
  31. SET Dirs=0
  32. SET Size=0
  33. FOR /F "tokens=*" %%A IN ('DIR /A /-C /N ^| MORE /E +5 ^| FIND /V "(s)" ^| EGREP -i %Rx%') DO CALL :Display "%%~A"
  34.  
  35. :: Add appropriate number of leading spaces to counted values
  36. IF %File% LSS       1000000 SET File= %File%
  37. IF %File% LSS        100000 SET File= %File%
  38. IF %File% LSS         10000 SET File= %File%
  39. IF %File% LSS          1000 SET File= %File%
  40. IF %File% LSS           100 SET File= %File%
  41. IF %File% LSS            10 SET File= %File%
  42. IF %Size% LSS 1000000000000 SET Size= %Size%
  43. IF %Size% LSS  100000000000 SET Size= %Size%
  44. IF %Size% LSS   10000000000 SET Size= %Size%
  45. IF %Size% LSS    1000000000 SET Size= %Size%
  46. IF %Size% LSS     100000000 SET Size= %Size%
  47. IF %Size% LSS      10000000 SET Size= %Size%
  48. IF %Size% LSS       1000000 SET Size= %Size%
  49. IF %Size% LSS        100000 SET Size= %Size%
  50. IF %Size% LSS         10000 SET Size= %Size%
  51. IF %Size% LSS          1000 SET Size= %Size%
  52. IF %Size% LSS           100 SET Size= %Size%
  53. IF %Size% LSS            10 SET Size= %Size%
  54. IF %Dirs% LSS       1000000 SET Dirs= %Dirs%
  55. IF %Dirs% LSS        100000 SET Dirs= %Dirs%
  56. IF %Dirs% LSS         10000 SET Dirs= %Dirs%
  57. IF %Dirs% LSS          1000 SET Dirs= %Dirs%
  58. IF %Dirs% LSS           100 SET Dirs= %Dirs%
  59. IF %Dirs% LSS            10 SET Dirs= %Dirs%
  60. SET File=         %File%
  61. SET Dirs=         %Dirs%
  62.  
  63. :: Capture last line of DIR's output and remove directory count
  64. FOR /F "tokens=1*" %%A IN ('DIR /-C') DO SET Free=%%B
  65.  
  66. :: Display "footer" for DIR command with corrected values
  67. ECHO.
  68. ECHO.%File% File^(s^)  %Size% bytes
  69. ECHO.%Dirs% %Free%
  70.  
  71. :: Done
  72. ENDLOCAL
  73. GOTO:EOF
  74.  
  75.  
  76. :: Display only the first 5 lines of the DIR command
  77. :Header
  78. :: Increment line counter
  79. SET /A File += 1
  80. :: Stop after first 5 lines
  81. IF %File% GTR 5 GOTO:EOF
  82. :: Remove line numbers, which were added to include empty lines
  83. FOR /F "tokens=1* delims=[]" %%K IN ('ECHO.%*') DO ECHO.%%~L
  84. GOTO:EOF
  85.  
  86.  
  87. :: Display files and keep track of total number of files, directories and size
  88. :Display
  89. :: Check if file or directory and update appropriate counters
  90. FOR /F "tokens=3" %%A IN ('ECHO.%1') DO IF "%%A"=="<DIR>" (
  91. 	SET /A Dirs += 1
  92. ) ELSE (
  93. 	SET /A File += 1
  94. 	SET /A Size += %%A
  95. )
  96. :: Escape redirection characters to prevent error messages
  97. SET Line=%1
  98. SET Line=%Line:<DIR>=^<DIR^>%
  99. :: Remove quotes, which were added to prevent error messages
  100. :: with unescaped redirection characters, and display result
  101. ECHO.%Line:"=%
  102. GOTO:EOF
  103.  
  104.  
  105. :: Display help screen
  106. :Syntax
  107. ECHO.
  108. ECHO RXDir.bat,  Version 0.50 beta for Windows NT 4 / 2000 / XP / Server 2003
  109. ECHO Use Regular eXpressions in the DIR command.
  110. ECHO.
  111. ECHO Usage:   RXDIR  ^<file_spec^>
  112. ECHO.
  113. ECHO Where:   ^<file_spec^>  is a regular expression specifying the files to display
  114. ECHO.
  115. ECHO Example: RXDIR  " AUTOEXEC\..{1,2}T$"
  116. ECHO.
  117. ECHO Notes:   1. This batch file uses EGREP to interpret the regular expression.
  118. ECHO             EGREP is available in many varieties on many sites.
  119. ECHO             This batch file has been tested with GNU grep 2.4.2, available
  120. ECHO             at http://unxutils.sourceforge.net/
  121. ECHO          2. File specifications are case insensitive.
  122. ECHO          3. All matching files will be displayed, including hidden files.
  123. ECHO          4. This batch file searches the current directory only.
  124. ECHO          5. The regular expression also matches the date, time and size parts
  125. ECHO             of DIR's output; examine the source to understand the limitations.
  126. ECHO.
  127. ECHO Written by Rob van der Woude
  128. ECHO http://www.robvanderwoude.com
  129.  

page last modified: 2024-04-16; loaded in 0.0086 seconds