【问题标题】:using the result of wmic path system32 estimatedchargeremaining as a variable使用 wmic path system32estimatedchargeremaining 的结果作为变量
【发布时间】:2014-04-10 08:42:13
【问题描述】:

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

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's”被忽略并且两个命令都被执行从而相互抵消了?

【问题讨论】:

  • 感谢马特的回复。我已经使用了您的建议如下... @echo ON setlocal ENABLEDELAYEDEXPANSION for /f "tokens=2 delims==" %%a in ('wmic path win32_battery estimatedchargeremaining /value^|find "="' ) 做(如果%%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 我得到的只是“无效动词”最后?
  • PS....对格式感到抱歉....我不能再以其他方式发布 4 小时??
  • 我编辑了您的问题以包含您在评论中发布的修改后的代码。这样更容易阅读。无效动词是 wmic 错误。如果你只运行 wmic path win32_battery get 会得到什么?您可以随时编辑您的问题以添加新的相关信息。越具体越好。

标签: batch-file path wmic batterylevel system32


【解决方案1】:

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

@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
)

【讨论】:

  • 嗨,别介意无效的动词,我对它进行了排序。但是....我现在正在使用这个......@echo ON cd c:/windows/system32 setlocal ENABLEDELAYEDEXPANSION for /f "tokens=2 delims==" %%a in ('wmic path win32_battery getestimatechargeremaining /value^|find "="' ) 做 (如果 %%a LEQ 15 GOTO ENABLE 如果 %%a GEQ 99 GOTO DISABLE ) :ENABLE DEVCON ENABLE "@ACPI\ACPI0003\2&DABA3FF&2" :DISABLE DEVCON DISABLE "@ACPI\ACPI0003 \2&DABA3FF&2" :END PAUSE 但是当我执行批处理文件时,启用和禁用命令都在执行,从而相互抵消?
【解决方案2】:

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

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 在其输出中包含文本,这会使其他所有内容复杂化。如果您必须使用其他命令,我不知道。

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2016-09-09
    • 2011-10-30
    • 2013-02-16
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多