1

Currently, I am trying to create a script file that will launch other programs if it detects that the laptop is running using its battery, not the AC.

Providing that I am using Windows 8.1.

I created a .bat file and I entered the following script:

@ECHO OFF
REM To Check the battery status, providing that 2 is connected to the AC
WMIC Path Win32_Battery Get BatteryStatus 
REM Check the content of battery status variable
IF NOT "%BatteryStatus%"=="2"(
echo laptop started to use its battery )

When I ran the above batch file its as if not detecting the content of BatteryStatus variable.

Can any one guide me to the best way to get batterystatus output in to battery status variable?

Another thing, I need a script to allow this bat to run and do this battery check every minute, how can I achieve that?

I tried to do it using task scheduler, but the problem that the maximum re occurrence time is 5 minute.

Any idea?

Thanks in advance!

Kirill
  • 1,530
  • 5
  • 24
  • 48
L22222
  • 21
  • 1
  • 7
  • 2
    Replace `WMIC Path Win32_Battery Get BatteryStatus` by `for /F "skip=1 tokens=1" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do set BatteryStatus=%%A`, because `WMIC` does _not_ set a variable... – aschipfl Jul 28 '15 at 23:10
  • 2
    Use `ping localhost -n 60 >nul` or `timeout /t 60 /nobreak` to pause the program for 60 seconds, after that, use a `goto` to create a loop with the code you want to run every 1 minute. – UnknownOctopus Jul 28 '15 at 23:12

4 Answers4

3

Okay, this is what I meant in my comment above:

@ECHO OFF
:LOOP
REM To Check the battery status, providing that 2 is connected to the AC
FOR /F "skip=1 tokens=1" %%A IN ('WMIC Path Win32_Battery Get BatteryStatus 2^>^&1') DO SET BatteryStatus=%%A
SET /A BatteryStatus+=0
REM Check the content of battery status variable
IF %BatteryStatus% EQU 2 (
ECHO laptop is running on AC power
) ELSE IF %BatteryStatus% GEQ 1 (
ECHO laptop started to use its battery
) ELSE ECHO unknown battery status
TIMEOUT /T 60 /NOBREAK > nul
GOTO :LOOP

This also features the loop as mentioned by this comment.

I added the 2>&1 portion to the WMIC command to redirect potential error messages as it might return a message like No Instance(s) Available.. The additional SET /A command immediately after ensures that variable %BatteryStatus% is never empty, even in case of said errors. There are additional checks to distinguish between running on battery, running on AC power and running in an unknown state (the former IF NOT "%BatteryStatus%"=="2" could be misleading in case of errors).

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

A little more robust, check that the variable is really batterystatus:

    for /F "delims== tokens=1,2" %%a in ('WMIC Path Win32_Battery Get BatteryStatus /format:textvaluelist.xsl') do @if "%%a"=="BatteryStatus" call :DoStuff %%b
    goto :EOF

:DoStuff
    do_thing1  >nul
    do_thing2 >nul
    do_thing3 >nul
    goto :EOF

Then, use a loop to continue the process:

:LOOP
for /f .....

timeout /t 60

goto :LOOP
joeking
  • 2,006
  • 18
  • 29
  • Wow, I've never heard of "timeout" before -- thanks for that one! – joeking Jul 28 '15 at 23:16
  • 1
    No problem, and just to be official here, the syntax should be `timeout /t 60` Also, `/nobreak` could be added to prevent press-key cancellation, and `>nul` could be added at the end to prevent output to console – UnknownOctopus Jul 28 '15 at 23:18
  • Thanks for your answer, but i how to add multiple process to run if the battery status is not equal 2 , i mean not only echo command that i want to execute if the battery status is anything except 2 , i have other commands like running other programs .. but not only echo, can you assist on this? – L22222 Jul 29 '15 at 00:09
  • also how to hide the console from showing up? where to add this >nul? – L22222 Jul 29 '15 at 00:12
  • I don't think you can hide the console entirely, but you can use "Start /b CMD /C MyBatchfile.bat" - which will create the new CMD window minimized. Well, if you use taskscheduler it has a "Hidden" option and "run whether the user is logged in or not" - which basically will run the process on a separate desktop from the logged in user. – joeking Jul 29 '15 at 00:14
  • @joeking You could run it in the background with a VBScript... http://stackoverflow.com/questions/1706075/run-bat-file-in-background – UnknownOctopus Jul 29 '15 at 00:49
  • i tried to echo the value of %%b by runing echo %%b command inside the dostaff and its not working, am i did it correctly ? – L22222 Jul 29 '15 at 07:45
  • Its a parameter in DoStuff, so use "%1". Try "call /?" for more details on what you can do with arguments. – joeking Jul 29 '15 at 18:51
0

This is how i make it work with my needs:

@ECHO OFF
:LOOP
REM To Check the battery status, providing that 2 is connected to the AC
for /F "delims== tokens=1,2" %%a in ('WMIC Path Win32_Battery Get BatteryStatus /format:textvaluelist.xsl') do @if "BatteryStatus"=="%%a" call :DoStaff "%%b"
:DoStaff
if  %~1 NEQ 2 (
ECHO laptop started to use its battery
) 
TIMEOUT /T 60 /NOBREAK > nul
GOTO :LOOP

I hope that i could hide the batch box some how, but anyway, thank you all for your help, its really appreciated!

Kind Regards

L22222
  • 21
  • 1
  • 7
0

Throwing this in as a potential answer to a minor part of the question. You can hide the window by adding a powershell line in your batch:

powershell -window hidden -command ""

Note that you will still see the initial cmd window flash at startup but it will immediately disappear and continue running in the background.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62