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. . . . . . . . . . . :

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 to 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.

  • If I have to export the output of ipconfig | findstr /i "ipv4" to a text file that perfectly acceptable. Feb 2, 2016 at 5:21
  • I'm not a Windows user, but you can use 'sed' or 'perl' or any other regexp processor to isolate those characters using a regular expression. The regular expression would be something like ^.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}.*$ and then you only retain the first capture group (within the parentheses, with \1 in sed for instance). Feb 2, 2016 at 6:04
  • 1
    @retrography Windows does not have sed, perl etc built-in. The OP's problem can be resolved with built-in commands (see my answer)
    – DavidPostill
    Feb 2, 2016 at 12:03

5 Answers 5


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 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. . . . . . . . . . . :"
  rem split on : and get 2nd token
  for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
    rem we have ""
    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


  • 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. . . . . . . . . . . :


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).
  • Thank you so much David! I tested the code out and it came out with exactly what I wanted. My only problem is I can't seem to use the variable !_3octet!, Please excuse my ignorance if I'm doing something wrong. Here is the first instance I will be using that variable. cd C:\Windows for /l %i IN (1,1,254) do ping -n 1 !_3octet!%i Instead of plugging in the variable command instead does a ping for "!_3octet!%i" with %i being every number 1-254 Feb 3, 2016 at 17:12
  • @SamuelPauk as instructed, replace rem add additional commands here in the batch file with for /l %%i in (1,1,254) do ping -n 1 !_3octet!%%i
    – DavidPostill
    Feb 3, 2016 at 18:26

This works for me on multiple computers:

for /F "tokens=14" %A in ('"ipconfig | findstr IPv4"') do echo %A
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 13, 2023 at 17:24

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


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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .