Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for battrun.bat

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

  1. @ECHO OFF
  2. :: Check Windows version (NT 4 or later)
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check Windows version (XP or later)
  9. VER | FIND "Windows NT"   >NUL && GOTO Syntax
  10. VER | FIND "Windows 2000" >NUL && GOTO Syntax
  11.  
  12. :: Check if WMIC.EXE is available
  13. WMIC.EXE Alias /?:Brief >NUL 2>&1 || GOTO Syntax
  14.  
  15. :: Check command line arguments: the first argument must be a file
  16. :: (script or executable), which may or may not be specified with
  17. :: an extension and/or a path, so we need to find the fully qualified
  18. :: path and file name first
  19. IF "%~1"=="" GOTO Syntax
  20. SET Prog="%~1"
  21. IF NOT EXIST %Prog% IF NOT EXIST "%~1.*" SET Prog="%~$PATH:1"
  22. IF %Prog%=="" IF "%~x1"=="" (
  23. 	FOR %%A IN (%PathExt%) DO (
  24. 		FOR %%B IN ("%~1%%A") DO IF !Prog!=="" SET Prog="%%~$PATH:B"
  25. 	)
  26. )
  27. IF NOT EXIST %Prog%          GOTO Syntax
  28. DIR /A-D %Prog% >NUL 2>&1 || GOTO Syntax
  29. SET Check=0
  30. FOR %%A IN (%PathExt%) DO (
  31. 	FOR %%B IN (%Prog%) DO (
  32. 		IF /I "%%~xB"=="%%~A" SET Check=1
  33. 	)
  34. )
  35. IF NOT "%Check%"=="1" GOTO Syntax
  36.  
  37. :: Variables to translate the returned BatteryStatus integer to a descriptive text
  38. SET BatteryStatus.1=discharging
  39. SET BatteryStatus.2=The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
  40. SET BatteryStatus.3=fully charged
  41. SET BatteryStatus.4=low
  42. SET BatteryStatus.5=critical
  43. SET BatteryStatus.6=charging
  44. SET BatteryStatus.7=charging and high
  45. SET BatteryStatus.8=charging and low
  46. SET BatteryStatus.9=charging and critical
  47. SET BatteryStatus.10=UNDEFINED
  48. SET BatteryStatus.11=partially charged
  49.  
  50. :: Read the battery status
  51. FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Battery Get BatteryStatus /Format:List ^| FIND "="') DO SET %%A
  52.  
  53. :: Check the battery status, and display a warning message if running on battery power
  54. IF NOT "%BatteryStatus%"=="2" (
  55. 	> "%~dpn0.vbs" ECHO MsgBox vbLf ^& "The laptop is currently running on its battery." ^& vbLf ^& vbLf ^& "The battery is !BatteryStatus.%BatteryStatus%!." ^& vbLf ^& vbLf ^& "Connect the laptop to the mains voltage if possible." ^& vbLf ^& " "^, vbWarning^, "Battery Warning"
  56. 	CSCRIPT //NoLogo "%~dpn0.vbs"
  57. 	DEL "%~dpn0.vbs"
  58. )
  59.  
  60. :: Run the command specified in the command line arguments, starting from its own directory
  61. FOR %%A IN (%Prog%) DO PUSHD "%%~dpA"
  62. START "" %*
  63. POPD
  64. ENDLOCAL
  65. GOTO:EOF
  66.  
  67.  
  68. :Syntax
  69. ECHO.
  70. ECHO BattRun.bat,  Version 1.00 for Windows XP Pro and later
  71. ECHO Checks if the computer is running on batteries before starting a program.
  72. ECHO Will display a warning message if the computer is running on battery power.
  73. ECHO.
  74. ECHO Usage:   BATTRUN  program  [ arguments ]
  75. ECHO.
  76. ECHO Where:   "program"    is the program to start after checking the battery status
  77. ECHO          "arguments"  are the optional command line arguments for "program"
  78. ECHO.
  79. ECHO Example: BATTRUN CMD /C PAUSE
  80. ECHO.
  81. ECHO Notes:   The program will be started in its own parent directory.
  82. ECHO          This batch file requires WMIC, which is available in Windows XP Pro,
  83. ECHO          Windows Server 2003 and Windows Vista.
  84. ECHO.
  85. ECHO Written by Rob van der Woude
  86. ECHO http://www.robvanderwoude.com
  87.  
  88. IF "%OS%"=="Windows_NT" ENDLOCAL
  89.  

page last uploaded: 2017-04-06, 13:33