以下自己琢磨的,谢谢各位指点 @echo OFF
del battery-report.html>nul 2>nul
del battery.ini>nul 2>nul
del n.ini>nul 2>nul
powercfg /batteryreport
setlocal EnableDelayedExpansion
cd /d %~dp0
set n=0
For /f "tokens=1* delims=:" %%i in ('Type battery-report.html^|Findstr /n /c:"Report generated"') do (
Set /a n=%%i+1
ECHO !n!>>n.ini
)
endlocal
set /p number=<n.ini
set /a numa=%number%-1
set /a numb=%number%+2
call :checknum %numa% %numb%
findstr "Battery" battery.ini &&echo fail>result.log||echo pass>result.log
exit
:checknum
setlocal enabledelayedexpansion
for /l %%m in (%1,1,%2) do (
set lineFlag=0
for /f "delims=" %%r in ('type battery-report.html') do (
set /a lineFlag+=1
if !lineFlag!==%%m (
set lineContent=%%r
set lineContent=!lineContent: =!
echo.!lineContent!>>battery.ini
)
)
)
endlocal
goto :eof================================================================
想复杂了,有现成代码,如下:
@echo off
wmic path Win32_Battery Get BatteryStatus | find /v "BatteryStatus" | find "2" >nul 2>&1
if "%errorlevel%" == "0" (echo 电源供电,交流供电) else (echo 电池供电,直流供电)
echo.
pause