7

I'm attempting to have cmd copy only a specific set of character from a command output. I've been able to select the line that the phrase exists in but from there I am lost. Here is my command so far.

ipconfig | findstr /i "ipv4"

I'm sure you are familiar with the ipconfig command and the findstr command. Basically I'm isolating the IPv4 address of the specific computer. This is what the above command.

IPv4 Address. . . . . . . . . . . : 192.168.1.75

I'd like to isolate "192.168.1." and have that saved to a variable to be used much later in a command. Say that I set "192.168.1." to variable a.

The next command I wish to run scans all IP addresses from 192.168.1.1 to 192.168.1.254 and save whichever are being used. As this, soon to be program, will be run on different computers at various locations the "192.168.1." will be different and I'm looking to isolate whatever cmd puts in it's location. So the aforementioned variable a could be but is not limited to, 192.168.10., 10.10.10., or even 1.1.1. depending on the situation. Here is the code I will be using.

FOR /L %i IN (1,1,254) DO ping -n 1 (the 'a' variable)%i | FIND /i "bytes=">>c:\ipaddresses.txt

or if seeing what the variable will look like helps.

FOR /L %i IN (1,1,254) DO ping -n 1 192.168.1.%i | FIND /i "bytes=">>c:\ipaddresses.txt

I am using a 64 bit windows 10 pro machine.

DavidPostill
  • 162,382

5 Answers5

6

I'd like to isolate "192.168.1." and have that saved to a variable

Use the following batch file (test.cmd):

@echo off
setlocal
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line 
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
  rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
  rem split on : and get 2nd token
  for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
    rem we have " 192.168.42.78"
    rem split on . and get 4 tokens (octets)
    for /f "tokens=1-4 delims=." %%c in ("%%b") do (
      set _o1=%%c
      set _o2=%%d
      set _o3=%%e
      set _o4=%%f
      rem strip leading space from first octet
      set _3octet=!_o1:~1!.!_o2!.!_o3!.
      echo !_3octet!
      )
    )
  )
rem add additional commands here
endlocal

Notes:

  • The value you want is saved in _3octet
  • Remove the echo which is there for debugging
  • Replace rem add additional commands here with your "network ping"

Example usage and output:

F:\test>ipconfig | findstr /i "ipv4"
   IPv4 Address. . . . . . . . . . . : 192.168.42.78

F:\test>test
192.168.42.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /f - Loop command against the results of another command.
  • ipconfig - Configure IP (Internet Protocol configuration)
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • setlocal - Set options to control the visibility of environment variables in a batch file.
  • variables - Extract part of a variable (substring).
DavidPostill
  • 162,382
1

This works for me on multiple computers:

for /F "tokens=14" %A in ('"ipconfig | findstr IPv4"') do echo %A
FrozT
  • 11
0

Try something like this. the "ipconfig /all > network_info.txt" just makes a txt file for the program

ipconfig /all > network_info.txt
type network_info.txt | findstr /v Stuff | findstr /v Etc > whatyouwant.txt
set /p Build=<whatyouwant.txt

This option is time consuming but incredibly simple. You would have to keep on doing

| findstr /v something

and replacing the word with what you want. You would have to decide how you would like to remove certain words from different lines but this would work if you want simple. I use this on outputs for commands to remove garbage that i dont want. You would ideally get the out put in a txt, and it would read the first line so you would only have one line of the txt. Hope i helped or helped anyone else who needed this answer. It is convoluted but you do get your answer in a txt as well if thats a plus

0

To get IPv4 address from ipconfig & assign the value to variable a:

for /f "delims=" %i in ('ipconfig ^| find "IPv4"') do set a=%i& echo !a:*: =!

To save just the 1st 3 octets:

for /f "delims=" %i in ('ipconfig ^| find "IPv4"') do set a=%i& set a=!a:*: =!
for %i in (%a%) do set a=%~ni
Zimba
  • 1,291
0

To assign IP addresses to successively numbered environment variables (IP1, IP2). For systems having multiple network adapters.

@echo off
setlocal enabledelayedexpansion
set _count=1
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
    for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
        for /f "tokens=*" %%c IN ('echo %%b') do (set IP!_count!=%%c & echo IP!_count!=%%c & set /a _count+= 1)
  )
)
set _count=
call :haltAndStore 2> nul

REM kludge: Forces batch processor to abort causing side-effect of persisting env vars. :haltAndStore ()

BSalita
  • 915