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

前端 未结 10 943
梦谈多话
梦谈多话 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 11:04

    I wrote the script that pings google.com every 5 seconds and logging results with current time. Here you can find output to variables "commandLineStr" (with indices)

    @echo off
    
    :LOOPSTART
    
    echo %DATE:~0% %TIME:~0,8% >> Pingtest.log
    
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET scriptCount=1
    FOR /F "tokens=* USEBACKQ" %%F IN (`ping google.com -n 1`) DO (
      SET commandLineStr!scriptCount!=%%F
      SET /a scriptCount=!scriptCount!+1
    )
    @ECHO %commandLineStr1% >> PingTest.log
    @ECHO %commandLineStr2% >> PingTest.log
    ENDLOCAL
    
    timeout 5 > nul
    
    GOTO LOOPSTART
    

提交回复
热议问题