Search code examples
batch-fileuuid

Store the UUID and IP of a machine in a file through batch file


I need to generate a text file with data of the UUID and IP of a machine through batch file, something like this:

CAA8A570-86FF-81E4-3398-0071C21A28CE 192.168.0.0

I used these commands but I can't figure out how to put the info in the same file.

wmic csproduct get "UUID" > C:\UUID.txt
ipconfig /all | find /i "phy" > C:\MAC.txt

Solution

  • It is possible to get the IP address and the UUID of a computer through batch file. However, it is unclear what you want to do with them. So, I will show you the way to get them and I will store their value in a variable. Do whatever you want then:

    @echo off
    rem Script to get the UUID and IP address of this computer.
    
    rem Get IP address:
    for /F "tokens=2 delims=(:" %%A IN ('ipconfig /all ^| findstr /c:"IPv4"') do (
        for /F "tokens=*" %%B IN ("%%A") do (
            rem Set the IP address into the IP_address variable:
            set "IP_address=%%B"
        )
    )
    
    rem Get UUID:
    for /F %%A IN ('(wmic csproduct get "UUID"^) ^| findstr /c:"-"') do (
        rem Set the UUID in the UUID variable:
        set "UUID=%%A"
    )
    
    rem Echo the results
    echo We have found that this computer has a UUID of %UUID%.
    echo We have also found the IP address of this computer. It is: %IP_address%
    

    If you want to redirect them to a file in the format mentioned in your question, use:

    (echo %UUID%  %IP_address%)>filename.txt
    

    And if you want to append them, use:

    (echo %UUID%  %IP_address%)>>filename.txt
    

    You may understand better how my code works, if I show you the output of each command processed.


    ipconfig /all | findstr /c:"Ipv4":

    The output of it, is (for me):

       IPv4 Address. . . . . . . . . . . : xxx.xxx.x.x(Preferred)
    

    I am not sure if you are getting these parenthesis.

    The for /F loop which processes the command has tokens=2 and delims=(: options.

    • delims=(: option means not to parse into tokens the characters ( and :. The tokens then, will be (separated by |): IPv4 Address. . . . . . . . . . . | xxx.xxx.x.x|Preferred). I have removed the characters (: as you can see. The token which contains the IP address is the second one, so I select it using:
    • tokens=2 option.

    Because there is one space in its start, I make another loop in this to remove the spaces in the start, using tokens=* option.

    Finally, the IP address is set into the IP_address variable and it is ready for use!


    (wmic csproduct get "UUID") | findstr /c:"-":

    The output of this command is just the UUID (no additional loops here):

    F5A91381-529E-11CB-A155-BF406BD05412
    

    So, I just set it to a variable (UUID) and it is ready for use!


    For seeing what 'redirect' and what 'append' means, I suggest the following links for reading:

    Also, I suggest opening a cmd, typing the following commands and reading carefully their output:

    • for /?
    • wmic /?
    • findstr /?
    • rem /?
    • set /?
    • echo /?

    which will surely help you understand my code.