Assign output of a program to a variable using a MS batch file

前端 未结 10 944
梦谈多话
梦谈多话 2020-11-22 10:06

I need to assign the output of a program to a variable using a MS batch file.

So in GNU Bash shell I would use VAR=$(application arg0 arg1). I need a si

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 11:04

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    REM Prefer backtick usage for command output reading:
    REM ENABLEDELAYEDEXPANSION is required for actualized
    REM  outer variables within for's scope;
    REM within for's scope, access to modified 
    REM outer variable is done via !...! syntax.
    
    SET CHP=C:\Windows\System32\chcp.com
    
    FOR /F "usebackq tokens=1,2,3" %%i IN (`%CHP%`) DO (
        IF "%%i" == "Aktive" IF "%%j" == "Codepage:" (
            SET SELCP=%%k
            SET SELCP=!SELCP:~0,-1!
        )
    )
    echo actual codepage [%SELCP%]
    
    ENDLOCAL
    

提交回复
热议问题