Set output of a command as a variable (with pipes)

前端 未结 6 1123
花落未央
花落未央 2020-12-01 05:09

Can you redirect the output of a command to a variable with pipes?

I haven\'t tried much as I haven\'t been able to think of anything to try, but I have tried one me

6条回答
  •  忘掉有多难
    2020-12-01 05:40

    In a batch file I usually create a file in the temp directory and append output from a program, then I call it with a variable-name to set that variable. Like this:

    :: Create a set_var.cmd file containing: set %1=
    set /p="set %%1=""%temp%\set_var.cmd"
    
    :: Append output from a command
    ipconfig | find "IPv4" >> "%temp%\set_var.cmd"
    call "%temp%\set_var.cmd" IPAddress
    echo %IPAddress%
    

提交回复
热议问题