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!
答案 0 :(得分:2)
Okay, this is what I meant in my comment above:
echo("Your order has been sent. Please
<a href=
'http://www.christmascakesforcancerresearch.com.au/order_form.html'>
click here</a> to go back and send through the money as a Direct Bank
Deposit otherwise <form action='https://www.paypal.com/cgi-bin/webscr'
method="'post' target='_top'>");
This also features the loop as mentioned by this comment.
I added the @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
portion to the 2>&1
command to redirect potential error messages as it might return a message like WMIC
.
The additional No Instance(s) Available.
command immediately after ensures that variable SET /A
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 %BatteryStatus%
could be misleading in case of errors).
答案 1 :(得分: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
答案 2 :(得分:0)
这就是我如何使它符合我的需求:
@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
我希望我可以隐藏批处理盒的方法,但无论如何,谢谢大家的帮助, 真的很感激!
亲切的问候
答案 3 :(得分:0)
将此作为对问题的一小部分的潜在答案。您可以在powershell
:
batch
行来隐藏窗口
powershell -window hidden -command ""
请注意,您仍会在启动时看到初始cmd窗口闪烁,但它会立即消失并继续在后台运行。