| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | Japt | 180731T162227Z | Shaggy |
| 150 | Excel VBA | 180731T153655Z | Taylor R |
| 546 | Java | 180802T170900Z | Magic Oc |
| 009 | Emacs Lisp | 180802T154730Z | Winny |
| 004 | Bash+acpi | 180730T221047Z | algmyr |
| 028 | PowerShell | 180730T201031Z | Adá |
| 078 | C win32 | 180801T072110Z | Felix Pa |
| 050 | JavaScript browser | 180731T105011Z | Night2 |
| 016 | Bash | 180730T202329Z | Okx |
| 133 | QPython for Android | 180731T194414Z | Sundar R |
| 080 | Python 3.6 + psutil | 180730T202233Z | mbomb007 |
Excel VBA, 150 bytes
Restricted to 32-Bit Windows Installs of office, because the windows Kernel32 call is not 64-bit pointer safe.
Outputs to the cell A1 on the ActiveSheet.
Declare Sub GetSystemPowerStatus Lib"kernel32"(f As t)
Type t
i As Integer
b As Byte
End Type
Sub d
Dim e As t
GetSystemPowerStatus e
[A1]=e.b
End Sub
-26 bytes thanks to flexible output
-7 bytes thanks to @Neil for using an Int instead of 2 bytes
Java, 546 bytes (Windows XP or Higher)
The powercfg utility came into play for WindowsXP, I have no idea what utilities were available prior to this. I must also be run as administrator. This is god-awful and I didn't make an extreme golfing attempt...
import java.io.*;import java.util.Scanner;public class J {public static void main(String[] args)throws Exception{Process p=Runtime.getRuntime().exec("powercfg energy");p.waitFor();Scanner s=new Scanner(new File("C:\\windows\\system32\\energy-report.html"));String x;double a=0,b=0;while(s.hasNextLine()){x=s.nextLine();if(x.contains("Design Capacity")){s.nextLine();b=Integer.parseInt(s.nextLine().replaceAll("\\D+",""));}else if(x.contains("Last Full Charge")){s.nextLine();a=Integer.parseInt(s.nextLine().replaceAll("\\D+",""));}}System.out.print(a/b*100);}}
Formatted / commented...
import java.io.*;
import java.util.Scanner;
public class J {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("powercfg energy"); // Run CMD sys32 app.
p.waitFor(); // Wait for it.
Scanner s = new Scanner(new File(
"C:\\windows\\system32\\energy-report.html")); // Read output.
String x;
double a = 0, b = 0;
while (s.hasNextLine()) {
x = s.nextLine();
if (x.contains("Design Capacity")) { // Find max capacity.
s.nextLine();
b = Integer.parseInt(s.nextLine().replaceAll("\\D+", ""));
} else if (x.contains("Last Full Charge")) { // Find current capacity.
s.nextLine();
a = Integer.parseInt(s.nextLine().replaceAll("\\D+", ""));
}
}
System.out.print(a / b * 100); // Calculate %.
}
}
Honestly I was more interested to see if it was even possible in Java.
Emacs Lisp, 9 bytes
(battery)
Shows current battery status in mini-buffer at the bottom (or to stdout when using --batch) in format such as: Power N/A, battery Unknown (99.1% load, remaining time N/A).
Should work on Windows, OSX, BSD , and Linux.
Bash+acpi, 43 4 bytes
New rules
acpi
output on form
Battery 0: Charging, 92%, 00:05:37 until charged
not sure if the rule change makes things more interesting, couldn't you dump any text that contains all strings 0 to 100 which would be valid output? Seems to lose the original intent if so.
Old rules
set `acpi`;echo Remaining battery is ${4%,}
output on form
Remaining battery is 92%
Don't ask me why set works like this, but it does.
PowerShell, 28 bytes
-26 (!) thanks to AdmBorkBork. Previous version -3 thanks to colsw.
((gwmi win32_battery)|% e*g)
gwmi is short for Get-WmiObject
|% takes the pattern e*g and finds the only matching property; estimatedChargeRemaining
C (win32, gcc i686-w64-mingw32), 78 bytes
#include<windows.h>
main(s){GetSystemPowerStatus(&s);printf("%hhu",s>>16);}
Abuses the win32 API by letting GetSystemPowerStatus() write "somewhere" on the stack, the interesting member is in the third byte, according to the SYSTEM_POWER_STATUS struct.
Unfortunately, the #include seems to be needed, probably because of calling conventions.
Example output:
> bat.exe
90
Displays 255 on systems without a battery.
JavaScript (browser), 78 77 50 bytes
(-1 byte thanks to Benoit Esnard)
(-27 bytes since the output format is flexible now)
navigator.getBattery().then(b=>alert(b.level*100))
JavaScript (browser), 46 bytes, by returning a promise
(Suggested by Shaggy, requires a header and footer)
f=
_=>navigator.getBattery().then(b=>b.level*100)
f().then(alert)
Note: Only works on Google Chrome >= 38 (desktop and Android) or any other browser that supports Battery API. FireFox has removed this API due to privacy concerns.
Example on Android (old output format):
Example on a Windows laptop (old output format):
Bash, 70 67 50 46 43 16 bytes
`</*/*/*/B*/c*y`
Outputs: <battery 0-100>: command not found
Reads the file /sys/class/power_supply/BAT1/capacity.
Tested using Ubuntu 18.04 on a LENOVO ideapad 500.
Takes a while (since it searches the entire filesystem) - but it saves a byte!
Old answer:
echo Remaining battery is `</*/*/*/B*/c*y`%
QPython for Android, 133 bytes
from androidhelper import*
d=Android()
d.batteryStartMonitoring()
d.dialogCreateAlert(str(d.batteryGetLevel()[1])+"%")
d.dialogShow()
The +"%" isn't strictly necessary since it uses the newer more flexible output format anyway, but it makes for slightly nicer output at just +4 bytes.
Python 3.6 + psutil, 80 bytes
import psutil
print(f"Remaining battery is {psutil.sensors_battery().percent}%")
If the machine has no battery (like on TIO), the program will throw an error, because psutil.sensors_battery() returns None.
This requires Python 3.6 for string interpolation.




