Set the result of a command in a variable

Solved
Bonjour,

I would like to know how to store the result of a command in a variable?
Here is the command in question: "wmic csproduct get identifyingnumber"
Since the result is on 2 lines, I only want to retrieve the second line in my variable.
Thank you in advance.

7 answers

  1. I finally managed to do what I wanted.
    Here is the code to obtain the serial number:
    FOR /F "tokens=2 delims==" %%B IN ('WMIC csproduct GET identifyingnumber /VALUE ^| FIND /I "IdentifyingNumber="') DO SET serial=%%B

    Here is the one for the PC model:
    FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Name /VALUE ^| FIND /I "Name="') DO SET machine=%%A


    Thanks Dubcek for your help.
    3
    1. I have Windows 7 pro x64.
      Here is the result of your command:

      1:IdentifyingNumber 2:XXXXXXXXXXXX


      So apparently there is no 3rd line.
      Strange.
      0
      1. Contributor
        Possible indeed, if there is an empty line, it is necessary to have the second to last one
        which Windows? I have XP
        what does the command display (with line numbering)
        wmic csproduct get identifyingnumber | findstr /n "." 
        put do @set t=%a so that the sets do not appear
        0
        1. The underlined lines are the result of the command; it is not me who put the SET.
          According to the result, there apparently should be 3 lines (one of them empty?), which would explain why in the end I get ECHO enabled instead of the serial number

          C:\Windows\system32>for /F "delims=" %a in ('wmic csproduct get identifyingnumber') do set t=%a  :\Windows\system32>set t=IdentifyingNumber :\Windows\system32>set t=XXXXXXXXXXX :\Windows\system32>set t= C:\Windows\system32>echo %t% Command ECHO enabled.


          Thank you for your help.
          0
          1. Contributor
            `%a contains each line, my command stores in t the last line, therefore the second one.
            remove your sets`
            0
            1. Hello,

              I'm getting this:

              C:\Windows\system32>for /F "delims=" %a in ('wmic csproduct get identifyingnumber') do set t=%a :\Windows\system32>set t=IdentifyingNumber :\Windows\system32>set t=XXXXXXXXXXX :\Windows\system32>set t= C:\Windows\system32>echo %t% ECHO command enabled.


              I think that since there are multiple lines, it redefines the variable for each line.
              Any ideas?

              Thanks again for your help.

              P.S.: XXXXXXXXXXXXX being the line I wish to retrieve.
              0
              1. Contributor
                hello
                try interactively
                for /F %%a "delims=" %%a in ('wmic csproduct get identifyingnumber') do set t=%%a echo %t% 
                in a .bat file, use %%a
                1