Announcement

Collapse
No announcement yet.

Batch - Store output of a command in a variable?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch - Store output of a command in a variable?

    I am writing a batch file and need to store the output of a command in a variable. I have been going round and round trying to do his properly but not getting very far.

    Does anyone know how to store and output of a command in a variable in Batch files?

  • #2
    Re: Batch - Store output of a command in a variable?

    Run the command in a For-Do loop.

    Use For /F
    - http://www.robvanderwoude.com/ntfor.html
    - http://www.ss64.com/nt/for_cmd.html

    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Batch - Store output of a command in a variable?

      for /f "usebackq token=*" ... was what I was trying but this is awkward and didn't do exactly what I wanted, which was to store a multi line output so that I could use/work on it..

      In the end I gave up on batch being too lame and wrote it in vbs, not quite as lame, but still no unix language...

      Comment


      • #4
        Re: Batch - Store output of a command in a variable?

        Is this what you need:

        Set objShell = CreateObject("WScript.Shell")
        Set objWshScriptExec = objShell.Exec("ipconfig /all")
        Set objStdOut = objWshScriptExec.StdOut

        '
        ' Skip first four lines
        '
        For i = 1 To 4
        strLine = objStdOut.ReadLine
        WScript.Echo strLine
        Next

        '
        ' Add date/time information
        '

        strCurrentTime = " Current Date/Time. . . . . . . . .: " & Now()
        WScript.Echo strCurrentTime

        '
        ' Display the rest of the output
        '

        While Not objStdOut.AtEndOfStream
        strLine = objStdOut.ReadLine
        WScript.Echo strLine
        Wend
        |
        +-- JDMils
        |
        +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
        |

        Comment


        • #5
          Re: Batch - Store output of a command in a variable?

          Yes thanks I realised this after my frustration with batch and rewrote the thing in vbs which has been working nicely since Saturday.

          Thanks for the tip though.

          (I'm not sure that first 1 to 4 lines skip is a good idea though, It might hang if there was ever no output or less than 4 lines. Instead I used the endofstream structure with regex matching to get the bits I wanted which is safer...)

          Comment

          Working...
          X