Using variables in cmd command


Green vegetables

I'm trying to make a batch program that takes variables from basic CMD commands. E.g:

c:\Users> ipconfig
.....
IPv4 Address ....... 123.456.7.89
.....

Suppose I want to make a batch program that just prints the IP address on the screen like:

@echo off
echo (IP Address Variable Here)
pause;

what should I do?

thank you for your time.

David Postiel

I want to make a batch program that just prints the IP address on the screen

Use the following batch file.

GetIPAddress.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. . . . . . . . . . . : 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"
    set _ip=%%b
    rem strip leading space
    set _ip=!_ip:~1!
    )
  )
echo %_ip%
endlocal

notes:

  • The desired value is stored in_ip

Usage example and output:

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

F:\test>GetIPAddress
192.168.42.78

Further reading

  • A-Z Index of Windows CMD Command Line - Excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed expansion will cause the variable to be expanded at execution time rather than at parsing time.
  • /f - loops commands against the result of another command.
  • ipconfig - configure IP (Internet Protocol Configuration)
  • set - Show, set or delete CMD environment variables. Changes made with SET will only persist during the current CMD session.
  • setlocal - Set options to control the visibility of environment variables in batch files.
  • variable - extracts a part (substring) of a variable.

Related


Using variables in cmd command

Green vegetables I'm trying to make a batch program that takes variables from basic CMD commands. E.g: c:\Users> ipconfig ..... IPv4 Address ....... 123.456.7.89 ..... Suppose I want to make a batch program that just prints the IP address on the screen like:

Variables in the firewall-cmd command

Dominica I'm trying to write a script in Bash to add some firewall rules. The variable I'm passing doesn't work, I think I'm doing the substitution wrong. firewall-cmd --add-rich-rule='rule family="ipv4" source address="$IP/32" port port=10000 protocol=tcp acc

How to include variables in cmd command?

Molly For example, I want the process "myexe.exe" to run on any host. So I want the hostname to be evaluated on each computer and inserted into the command. Ideally, I'd like all of this to be done in one line. I tried the following code. mpiexec -n 8 -hosts 1

How to include variables in cmd command?

Molly For example, I want the process "myexe.exe" to run on any host. So I want the hostname to be evaluated on each computer and inserted into the command. Ideally, I'd like all of this to be done in one line. I tried the following code. mpiexec -n 8 -hosts 1

How to include variables in cmd command?

Molly For example, I want the process "myexe.exe" to run on any host. So I want the hostname to be evaluated on each computer and inserted into the command. Ideally, I'd like all of this to be done in one line. I tried the following code. mpiexec -n 8 -hosts 1

Pass variables in cmd using python

Honshitz I also want to pass some variables in command prompt along with some other text. I try to use this code but it doesn't work. Any hints that I might be doing something wrong, or how I should be doing it? There is a variable "v" that stores the URL and

Pass variables in cmd using python

Honshitz I also want to pass some variables in command prompt along with some other text. I try to use this code but it doesn't work. Any hints that I might be doing something wrong, or how I should be doing it? There is a variable "v" that stores the URL and

How to create a command with variables to be executed in cmd

pull Let me specify a question. I want to issue commands so when the user types eg. go (something)It will use goto specify the function somethingand find what the program should do when the variable equals something. If anything is unclear, just ask, I know my

How to create a command with variables to be executed in cmd

pull Let me specify a question. I want to issue commands so when the user types eg. go (something)It will use goto specify the function somethingand find what the program should do when the variable equals something. If anything is unclear, just ask, I know my

Using variables on the command line

Vasyl Kozhushko I wrote a piece of code and it works fine, but I need to use variables instead of static numbers in ranges 8 and 16 cat /etc/passwd | sed '/^#/d' | sed -n 'n;p' | sed 's/:\(.*\) //g' | sed 's/ /,/g' | sed 's/\(.*\),/\1./' | sort -r | sed 's/*ra

Using variables in the tail command

Palmer I'm trying to export characters from a reference file whose byte positions are known. For this, I have a long string of numbers stored as variables, which are used as input to tail commands. For example, the reference file looks like this: ggaaatgcattca

Using variables on the command line

Vasyl Kozhushko I wrote a piece of code and it works fine, but I need to use variables instead of static numbers in ranges 8 and 16 cat /etc/passwd | sed '/^#/d' | sed -n 'n;p' | sed 's/:\(.*\) //g' | sed 's/ /,/g' | sed 's/\(.*\),/\1./' | sort -r | sed 's/*ra

Using variables in sed command

Ozcan I am trying to add some text (path) to the end of the line found by the sed command: var="/folder1/folder2/folder3" sed -i "/Begins with this text/s/$/$var/" filename I know double quotes are required when using variables in the sed command, but if I u

Using variables in the tail command

Palmer I'm trying to export characters from a reference file whose byte positions are known. For this, I have a long string of numbers stored as variables, which are used as input to tail commands. For example, the reference file looks like this: ggaaatgcattca

Using variables in the `sed` command

Mayur Sawant I am using this command to copy certain lines from one file to another and it is working fine without issue. sed -f <(sed -e '1,10d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt The problem is that instead of

Using variables in the tail command

Palmer I'm trying to export characters from a reference file whose byte positions are known. For this, I have a long string of numbers stored as variables, which are used as input to tail commands. For example, the reference file looks like this: ggaaatgcattca

Using variables on the command line

Vasyl Kozhushko I wrote a piece of code and it works fine, but I need to use variables instead of static numbers in ranges 8 and 16 cat /etc/passwd | sed '/^#/d' | sed -n 'n;p' | sed 's/:\(.*\) //g' | sed 's/ /,/g' | sed 's/\(.*\),/\1./' | sort -r | sed 's/*ra

Using variables in the tail command

Palmer I'm trying to export characters from a reference file whose byte positions are known. For this, I have a long string of numbers stored as variables, which are used as input to tail commands. For example, the reference file looks like this: ggaaatgcattca

Using variables in the tail command

Palmer I'm trying to export characters from a reference file whose byte positions are known. For this, I have a long string of numbers stored as variables, which are used as input to tail commands. For example, the reference file looks like this: ggaaatgcattca

Using variables in the shutdown command

Winemaker 15 The goal of my script is to get input from the user how much time they want before closing, and the message they want to display during the closing process. My problem is that I can't figure out exactly how to put the variable into the shutdown co

Setting environment variables using cmd in windows

Mahendran To start my project in windows I have to set java environment variables in cmd using set JAVA_HOME = D:\Java\jdk1.7.0 But it didn't set the environment variable, I didn't know the problem was after a few hours, I found out the reason was the whitesp

How to use environment variables in CMD using Java?

Andrew I am new to using ProcessBuilderobjects to run commands in Java . My problem is that when I put the environment variable in builder.command(...)the method it doesn't work. However, when I hardcode the string of the environment variable, it works fine. B

Setting environment variables using cmd in windows

Mahendran To start my project in windows I have to set java environment variables in cmd using set JAVA_HOME = D:\Java\jdk1.7.0 But it didn't set the environment variable, I didn't know the problem was after a few hours, I found out the reason was the whitesp

Setting environment variables using cmd in windows

Mahendran To start my project in windows I have to set java environment variables in cmd using set JAVA_HOME = D:\Java\jdk1.7.0 But it didn't set the environment variable, I didn't know the problem was after a few hours, I found out the reason was the whitesp