Remove text with delimiter from a .txt and save it in a variable (Batch Windows)

0

Good to all the problem in itself is that I need to remove the ip from this line and save it in a variable ( only the IP 4 octets, it could be that ip or one like 192.168.100.2).

I have the following file .bat;

@echo off
echo            ============================
echo            = CONFIGURACION DEL EQUIPO =
echo            ============================
echo\
echo  Fecha de hoy: %date% 
echo.
echo  Nombre del Equipo: %computername% 
echo.
echo  Direccion IP: 
    ipconfig | find /i "IPv4"
    ipconfig | find /i "IPv4" >> ip.txt

echo.
echo  MAC del Equipo:
    getmac  
echo.
echo FECHA : %date% HORA : %time% NOMBRE DEL EQUIPO : %computername% >> info.txt

echo\
echo  Presione la tecla "SPACE" para cerrar esta ventana...
pause > nul

What he gives me is basic information of the team:

And what I want is the Ip to be added to the file to the file that I redirect as "info.txt" that is shown in the following way.

In summary I want to get the string that goes from ":" onwards and save it in a variable. Thanks in advance.

    
asked by Blitzinger 24.05.2018 в 13:11
source

1 answer

0

You can use the for / F command to run the code that interests you and retrieve the value.

It would be something like this:

for /f "usebackq tokens=2 delims=:" %%f in ('ipconfig ^| find /i "IPv4"') do set ip=%%f

echo %ip%

The first line executes ipconfig | find /i "IPv4" and returns it to the for command that chops it into parts using the : separator. The part that interests you is the second where the IP is. In the part of DO is assigned to a variable that you use to save the IP.

In case you have more than one IP, it will return the last one.

I hope it serves you.

    
answered by 24.05.2018 в 18:08