Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for logbatch.bat

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

  1. @ECHO OFF
  2. :: Check for:
  3. :: correct Windows version
  4. IF NOT "%OS%"=="Windows_NT"   GOTO Syntax
  5. :: at least 1 command line argument
  6. IF "%~1"=="" GOTO Syntax
  7. :: /? or -? as the first command line argument
  8. ECHO "%~1" | FIND "?" >NUL && GOTO Syntax
  9. :: if the batch file to be tested exists
  10. IF NOT EXIST "%~1" GOTO Syntax
  11.  
  12. CLS
  13. ECHO.
  14. ECHO User Name       : %UserName%
  15. ECHO Computer Name   : %ComputerName%
  16. ECHO User Domain     : %UserDomain%
  17. FOR /F "tokens=*" %%A IN ('VER') DO ECHO Windows Version : %%A
  18. ECHO Time and Date   : %Date%, %Time%
  19. ECHO.
  20. ECHO Command         : %*
  21.  
  22. :: Enable delayed variable expansion
  23. SETLOCAL ENABLEDELAYEDEXPANSION
  24.  
  25. :: In case this batch file is started from
  26. :: an UNC path, map a drive letter first
  27. PUSHD "%~dp1"
  28.  
  29. :: Remove the line containing the ECHO OFF command
  30. TYPE "%~1" | FIND /I /V "ECHO OFF" > "%~dpn1_Test%~x1"
  31.  
  32. :: Collect the command line arguments for
  33. :: the batch file that is going to be tested
  34. SET Args=
  35. SET Dummy=
  36. IF NOT "%~2"=="" (
  37. 	FOR %%A IN (%*) DO (
  38. 		IF "!Dummy!"=="" (
  39. 			SET Dummy=%%A
  40. 		) ELSE (
  41. 			SET Args=!Args! %%A
  42. 		)
  43. 	)
  44. )
  45.  
  46. :: Run the "stripped" temporary copy of the batch
  47. :: file with its arguments and log every command
  48. ECHO ON
  49. @CALL "%~dpn1_Test%~x1"%Args% > "%~dpn1_Test.log" 2>&1
  50. @SET ReturnCode=%ErrorLevel%
  51. @ECHO OFF
  52.  
  53. :: Remove the "stripped" temporary batch file
  54. DEL "%~dpn1_Test%~x1"
  55.  
  56. :: Display return code and log file name
  57. ECHO.
  58. ECHO Return Code     : %ReturnCode% ^(maybe^)
  59. ECHO Log File        : %~dpn1_Test%~x1
  60.  
  61. :: Remove the drive mapping
  62. POPD
  63.  
  64. :: Purge the local settings
  65. ENDLOCAL
  66.  
  67. :: Wait 1 minute if the batch file was started by doubleclicking
  68. IF /I "%~0"=="%~f0" PING 127.0.0.1 -n 60 >NUL 2>&1
  69.  
  70. :: Done
  71. GOTO:EOF
  72.  
  73.  
  74. :Syntax
  75. ECHO LogBatch.bat,  Version 1.00 for Windows 2000 and later
  76. ECHO Run a batch file and log each individual command line and its result
  77. ECHO.
  78. ECHO Usage:  LOGBATCH.BAT  some_bat.bat  [ optional arguments for some_bat ]
  79. ECHO.
  80. ECHO Where:  "some_bat.bat" is the batch file to be logged
  81. ECHO.
  82. ECHO Note:   "some_bat.bat" may have either a .bat or .cmd extension,
  83. ECHO         but it MUST be a batch file, not an executable.
  84. ECHO.
  85. ECHO Issues: LogBatch.bat MAY fail on switches like ^/? or ^/A in
  86. ECHO         some_bat.bat's optional command line arguments.
  87. ECHO         The displayed value for the return code is not reliable.
  88. ECHO.
  89. ECHO Written by Rob van der Woude
  90. ECHO http://www.robvanderwoude.com
  91.  

page last uploaded: 2017-08-21, 14:26