Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rc.bat

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

  1. @ECHO OFF
  2. IF "%~1"=="/?" GOTO Syntax
  3.  
  4. IF "%~1"=="" (
  5. 	GOTO :Skip
  6. ) ELSE (
  7. 	SETLOCAL ENABLEDELAYEDEXPANSION
  8. 	FOR /F "tokens=*" %%A IN ('VER ^| TIME') DO (
  9. 		IF "!TimeBefore!"=="" (
  10. 			SET TimeBefore=%%A
  11. 			SET TimeBefore=!TimeBefore:,=.!
  12. 			FOR %%B IN (!TimeBefore!) DO (
  13. 				SET TimeBefore=%%B
  14. 			)
  15. 		)
  16. 	)
  17. )
  18.  
  19. (SET CALL=)
  20. :: Note the trailing space after CALL=CALL
  21. IF /I "%~x1"==".bat" (SET CALL=CALL )
  22. IF /I "%~x1"==".cmd" (SET CALL=CALL )
  23. %CALL%%*
  24. (SET CALL=)
  25.  
  26. :Skip
  27. SET RC=%ErrorLevel%
  28. IF NOT "%~1"=="" (
  29. 	FOR /F "tokens=*" %%A IN ('VER ^| TIME') DO (
  30. 		IF "!TimeAfter!"=="" (
  31. 			SET TimeAfter=%%A
  32. 			SET TimeAfter=!TimeAfter:,=.!
  33. 			FOR %%B IN (!TimeAfter!) DO (
  34. 				SET TimeAfter=%%B
  35. 			)
  36. 		)
  37. 	)
  38. )
  39.  
  40. ECHO.
  41. ECHO [RC=%RC%]
  42. IF "%~1"=="" (
  43. 	SET RC=& EXIT /B %RC%
  44. ) ELSE (
  45. 	FOR /F "tokens=1-4 delims=:.," %%A IN ("%TimeBefore%") DO (
  46. 		SET HoursBefore=%%A
  47. 		SET MinutesBefore=%%B
  48. 		SET SecondsBefore=%%C
  49. 		SET FractBefore=%%D
  50. 	)
  51. 	FOR /F "tokens=1-4 delims=:.," %%A IN ("%TimeAfter%") DO (
  52. 		SET HoursAfter=%%A
  53. 		SET MinutesAfter=%%B
  54. 		SET SecondsAfter=%%C
  55. 		SET FractAfter=%%D
  56. 	)
  57. 	FOR %%A IN (HoursAfter MinutesAfter SecondsAfter FractAfter HoursBefore MinutesBefore SecondsBefore FractBefore) DO CALL :RemoveLeadingZero %%A
  58. 	SET /A Hours   = !HoursAfter!   - !HoursBefore!
  59. 	SET /A Minutes = !MinutesAfter! - !MinutesBefore!
  60. 	SET /A Seconds = !SecondsAfter! - !SecondsBefore!
  61. 	SET /A Fract   = !FractAfter!   - !FractBefore!
  62. 	SET /A TimeDif =  100 * !Hours!   + !Minutes!
  63. 	SET /A TimeDif =  100 * !TimeDif! + !Seconds!
  64. 	SET /A TimeDif = 1000 * !TimeDif! + 10 * !Fract!
  65. 	ECHO [Time=!TimeDif! ms]
  66. 	ENDLOCAL & EXIT /B %RC%
  67. )
  68.  
  69.  
  70. :RemoveLeadingZero
  71. SET TempVar=!%1!
  72. IF "%TempVar:~0,1%"=="0" SET TempVar=%TempVar:~1%
  73. IF "%TempVar:~0,1%"==""  SET TempVar=0
  74. SET %1=%TempVar%
  75. GOTO:EOF
  76.  
  77.  
  78. :Syntax
  79. ECHO.
  80. ECHO RC.bat,  Version 2.11 for Windows NT
  81. ECHO Display a command's return code ("errorlevel") and execution time
  82. ECHO.
  83. ECHO Usage:  RC  command  parameters
  84. ECHO    or:  command  parameters ^&  RC
  85. ECHO.
  86. ECHO Written by Rob van der Woude
  87. ECHO http://www.robvanderwoude.com
  88.  

page last uploaded: 2017-07-06, 12:37