Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for battstat.bat

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

  1. @ECHO OFF
  2. :: Check for Windows NT 4 or later (though XP or later is required)
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables
  6. SETLOCAL
  7.  
  8. :: No command line arguments required
  9. IF NOT "%~1"=="" GOTO Syntax
  10.  
  11. :: Check if WMIC.EXE is available
  12. WMIC.EXE Alias /?:Brief >NUL 2>&1 || GOTO Syntax
  13.  
  14. :: Use WMI to retrieve battery status information
  15. FOR /F "tokens=1* delims==" %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryStatus              Get Charging^,Critical^,Discharging /Format:list ^| FIND "=TRUE"') DO ECHO Battery is %%A
  16. FOR /F "tokens=*  delims="  %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryStatus              Get PowerOnline^,RemainingCapacity  /Format:list ^| FIND "="')     DO SET  Battery.%%A
  17. FOR /F "tokens=*  delims="  %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryRuntime             Get EstimatedRuntime                /Format:list ^| FIND "="')     DO SET  Battery.%%A
  18. FOR /F "tokens=*  delims="  %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryFullChargedCapacity Get FullChargedCapacity             /Format:list ^| FIND "="')     DO SET  Battery.%%A
  19.  
  20. :: Calculate runtime left and capacity
  21. SET /A Battery.EstimatedRuntime  = ( %Battery.EstimatedRuntime% + 30 ) / 60
  22. SET /A Battery.RemainingCapacity = ( %Battery.RemainingCapacity%00 + %Battery.FullChargedCapacity% / 2 ) / %Battery.FullChargedCapacity%
  23.  
  24. :: Display results
  25. IF /I "%Battery.PowerOnline%"=="TRUE" (
  26. 	ECHO Now working on mains power
  27. 	ECHO Battery %Battery.RemainingCapacity%%% charged
  28. ) ELSE (
  29. 	ECHO Estimated remaining runtime %Battery.EstimatedRuntime% minutes
  30. 	ECHO Remaining capacity %Battery.RemainingCapacity%%%
  31. )
  32. GOTO:EOF
  33.  
  34.  
  35. :Syntax
  36. ECHO.
  37. ECHO BattStat.bat,  Version 1.00 for Windows XP Pro or later
  38. ECHO Display current battery status for the local computer
  39. ECHO.
  40. ECHO Usage:  BATTSTAT
  41. ECHO.
  42. ECHO Note:   Uses WMIC to query the battery status;
  43. ECHO         WMIC is native in Windows XP Professional,
  44. ECHO         Windows Server 2003 and Windows Vista.
  45. ECHO.
  46. ECHO Written by Rob van der Woude
  47. ECHO http://www.robvanderwoude.com
  48.  
  49. :: End localization
  50. IF "%OS%"=="Windows_NT" ENDLOCAL
  51.  

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