1

I am making a simple CMD .bat file to quickly ping ip adresses i've already saved.

I can set /p ip = Enter IP: but i dont wish to input ip every time. I tried set ip = 192.168.0.1 but it says "IP address must be specified" when i try ping %ip%. I tried set ip = www.google.com too and it doesnt work either.

It looks a little like this:

echo 2 - use 192.168.0.1
SET /P izbira=Choose:
if %izbira% == 2 goto use_01

:use_01
set ip = 192.168.0.1
goto ping

:ping
ping %ip%

What am I doing wrong?

1 Answers1

3

You have spaces between ip = 192.168.0.1. This will cause this command to set a variable called %ip % and give it the value  192.168.0.1. This is why you need to get rid of the spaces. You should also use double quotes to make sure you don't include trailing spaces, so you should use this:

echo 2 - use 192.168.0.1
SET /P izbira=Choose:
if "%izbira%"=="2" goto use_01

:use_01
set "ip=192.168.0.1"
goto ping

:ping
ping %ip%

I should also note that is best to compare strings with doublequotes around them too, and that your program will always execute the ping, because even if the goto doesn't get executed, it will just continue, still going to the set and the ping.

Dennis van Gils
  • 3,287
  • 2
  • 11
  • 35
  • Thank you, now it works. I used c# a lot lately and I guess it just got me off the path. – Kristjan1215 May 11 '16 at 15:10
  • @Kristjan1215 If my answer solved your problem, you should consider clicking the check to the left of it so people searching on google for the same problem can find it. See [this](//stackoverflow.com/help/someone-answers) for why this is important. – Dennis van Gils May 11 '16 at 15:50
  • I did. An will from now on. And i will read the rules... I did try upvoting your comment but i do not have enough reputation. – Kristjan1215 May 11 '16 at 17:34