0

在批处理文件中,我正在运行命令:

wmic path system32 estimatedchargeremaining

我想要实现的是使用它的结果来执行另一个依赖于结果的命令。例如,如果输出显示为:

estimatedchargeremaining 
75

我想使用 75...不是错误级别 0

所以之后在那里运行的命令将类似于:

    if estimatedchargeremaining LEQ 15 
*NEW COMMAND*

if estimatedchargeremining GTR 99
*NEW COMMAND*

但是由于返回的值不满足其中任何一个,因此不会执行任何命令。

我希望这足够清楚。不幸的是,由于我目前在另一台计算机上,所以我无法向您展示我已经拥有的东西。但如果需要,可以稍后提供。

干杯伙计们

这是格式化的代码:

@echo ON 
setlocal ENABLEDELAYEDEXPANSION 
for /f "tokens=2 delims==" %%a in ( 
  'wmic path win32_battery estimatedchargeremaining /value^|find "="' ) do ( 
    if %%a LEQ 15 echo DEVCON ENABLE "@ACPI\ACPI0003\2&DABA3FF&2" GOTO END 
    if %%a GTR 99 echo DEVCON DISABLE "@ACPI\ACPI0003\2&DABA3FF&2" GOTO END 
) 
:end
PAUSE

这是我现在尝试使用以下 matts 建议的代码:

    @echo ON
cd c:/windows/system32
setlocal enableextensions

for /f "tokens=2 delims==" %%a in (
  'wmic path win32_battery get estimatedchargeremaining /value^|find "="'
) do (
  if %%a LEQ 15 (GOTO :ENABLE) else goto :DISABLE0
:DISABLE0
  if %%a GEQ 99 (GOTO :DISABLE)
)

:ENABLE
DEVCON ENABLE "@ACPI\ACPI0003\2&DABA3FF&2" 

:DISABLE
DEVCON DISABLE "@ACPI\ACPI0003\2&DABA3FF&2" 


:END
PAUSE

当我执行批处理文件时,这就是发生的事情:

C:\Users\Aaron\Desktop>cd c:/windows/system32

c:\Windows\System32>setlocal enableextensions

c:\Windows\System32>for /F "tokens=2 delims==" %a in ('wmic path win32_battery g et estimatedchargeremaining /value|find "="') do ( if %a LEQ 15 (GOTO :ENABLE )  else goto :DISABLE0  if %a GEQ 99 (GOTO :DISABLE ) )

c:\Windows\System32>(  LEQ 15 (GOTO :ENABLE )  else goto :DISABLE0  GEQ 99 (GOTO :DISABLE ) )

c:\Windows\System32>DEVCON ENABLE "@ACPI\ACPI0003\2&DABA3FF&2" ACPI\ACPI0003\2&DABA3FF&2                                   : Enabled 1 device(s) enabled.

c:\Windows\System32>DEVCON DISABLE "@ACPI\ACPI0003\2&DABA3FF&2" ACPI\ACPI0003\2&DABA3FF&2                                   : Disabled 1 device(s) disabled.

c:\Windows\System32>PAUSE Press any key to continue . . .

我无法弄清楚为什么“GOTO”被忽略并且两个命令都被执行从而相互抵消了?

4

2 回答 2

0

尝试这个。我这里没有笔记本电脑可以测试,但应该很近。

@echo off
setlocal

for /f "tokens=2 delims==" %%a in (
  'wmic path win32_battery get estimatedchargeremaining /value^|find "="'
) do (
  if %%a LEQ 15 echo do something
  if %%a GTR 99 echo do something else
)
于 2014-04-10T12:26:05.483 回答
0

我知道这个问题被问到已经很久了,但我遇到了类似的问题,谷歌把我带到了这里。所以这是我的解决方案:

powershell -command "(Get-WmiObject Win32_Battery).EstimatedChargeRemaining" >batdat.txt
FOR /F "tokens=* delims=" %%x in (batdat.txt) DO set /A var=%%x
rem: After that, you can use var for whatever conditions you want.

注意:此解决方案涉及使用完全不同的命令,因为上面提到的命令只给出数值,与wmic path system32 estimatedchargeremaining输出中的文本不同,这会使其他所有内容复杂化。如果您必须使用其他命令,我不知道。

于 2021-06-24T11:22:08.203 回答