我想创建一个这样的简单批处理文件:
SET HibernateEnabled=[getHibernationStatus]
IF HibernateEnabled==1
ECHO do things
ELSE
ECHO do other things
此外,我不想使用 Windows Powershell。有什么建议吗?
答案1
Windows 10 64 位
使用 cmd、for in do、reg query 和 find 确定休眠注册表值并使用 if 根据结果采取行动。
命令:
cmd /q /e
FOR /f "tokens=3" %g in ('reg query HKLM\SYSTEM\ControlSet001\Control\Power\ ^|FIND /i "HibernateEnabled "') do if %g==0x1 (
echo.
echo hibernation enabled
echo do things
) else (
echo.
echo hibernation disabled
echo do other things
)
脚本:
@echo off
setlocal enableextensions
FOR /f "tokens=3" %%g in ('reg query HKLM\SYSTEM\ControlSet001\Control\Power\ ^|FIND /i "HibernateEnabled "') do if %%g==0x1 (
echo.
echo hibernation enabled
echo do things
) else (
echo.
echo hibernation disabled
echo do other things
)
pause
exit /b
答案2
您还可以通过以下方式确定powercfg /query
:
powercfg /query | find /I "Current AC Power Setting Index: 0x00000000" >nul 2>&1
if %errorlevel% equ 0 (
powercfg /query | find /I "Current DC Power Setting Index: 0x00000000" >nul 2>&1
if %errorlevel% equ 0 (
REM HIBERNATION IS DISABLED
) Else (
REM HIBERNATION IS ENABLED
)
)