【问题标题】:Do a cmd command in a msgbox在 msgbox 中执行 cmd 命令
【发布时间】:2021-02-01 05:22:54
【问题描述】:

所以,我的目标是获得一个 msgbox,它可以告诉我我正在使用的电源计划,而无需使用命令提示符。

我尝试过制作批处理文件,但只执行命令。

start cmd /k powercfg/getactivescheme

当我尝试从命令提示符处执行此操作时,它只是在 msg 框中输出命令的文本,而不是实际输出。

msg %username% powerconfg/getactivescheme

提前致谢

【问题讨论】:

标签: batch-file cmd msgbox


【解决方案1】:

像这样尝试(使用for 在变量中获取命令输出):

@echo off
for /f "tokens=* delims= " %%a in ('powercfg /getactivescheme') do msg * %%a

使用 VBScript MsgBox 的一个更好的例子,它可以有自定义的标题和图标:

@echo off
for /f "tokens=* delims= " %%a in ('powercfg /getactivescheme') do set "body=%%a"
echo MsgBox "%body%",64,"YOUR TITLE" >temp.vbs
cscript //nologo temp.vbs
del temp.vbs

Learn more on VBScript MsgBox

【讨论】:

    【解决方案2】:

    你也可以试试这个批处理文件:


    @echo off
    Set Title="command in a msgbox"
    Title %Title%
    @for /f "delims=" %%a in ('powercfg /getactivescheme') do (Set "MSG=%%a")
    >"%temp%\%~n0.vbs" ( echo MsgBox "%MSG%",vbInformation,%Title% )
     wscript "%temp%\%~n0.vbs" & Del "%temp%\%~n0.vbs"
    

    编辑:如果您想在两个括号 ( Data Info....) 之间提取数据,您可以使用像这样处理 Regex 的 vbscript:

    例如在我的法国机器测试中( Utilisation Normale )


    @echo off
    Set Title="command in a msgbox"
    Title %Title%
    @for /f "delims=" %%a in ('powercfg /getactivescheme') do (Set "MSG=%%a")
    Call :ExtractData "%MSG%" MSG
    >"%temp%\%~n0.vbs" ( echo MsgBox "%MSG%",vbInformation,%Title% )
    Wscript.exe "%temp%\%~n0.vbs" & Del "%temp%\%~n0.vbs"
    Exit
    REM------------------------------------------------------------------------
    :ExtractData
    (
        echo wscript.echo Extract("%~1"^)
        echo Function Extract(Data^)
        echo Dim strPattern,oRegExp,Matches
        echo strPattern = "(?:\()(.*)(?:\))"
        echo Set oRegExp = New RegExp
        echo oRegExp.IgnoreCase = True 
        echo oRegExp.Pattern = strPattern
        echo set Matches = oRegExp.Execute(Data^) 
        echo If Matches.Count ^> 0 Then Extract = Matches(0^).SubMatches(0^)
        echo End Function
    )>"%tmp%\%~n0.vbs"
    @for /f "delims=" %%a in ('cscript //nologo "%tmp%\%~n0.vbs"') do ( set "%2=%%a")
    If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
    Exit /B
    REM------------------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多