-5

I want to make a CMD batch file that uses user input to ping a IP.

I also want to integrate the feature of using user input to ping an IP Address into a menu so that you can choose weather you want to ping the IP Address only 4 times or endlessly.

Aidan
  • 1
  • 1
  • 6
  • User input? https://superuser.com/questions/837344/how-to-accept-user-input-command-in-batch-file – OneCricketeer Jul 08 '18 at 14:37
  • I will try this thanks. – Aidan Jul 08 '18 at 14:38
  • @cricket_007 Okay it works but not the way I want it to. All I want the user to do is type the IP Address and not the entire command, is there a way to make **set /p** run **ping -t .....** so that when you type the IP Address out it runs **ping -t .....** – Aidan Jul 08 '18 at 15:01
  • That's just accepting text. You can edit however you want. For example try `ping -t %CommandVar%` – OneCricketeer Jul 08 '18 at 15:03
  • Also, please don't delete your post again – OneCricketeer Jul 08 '18 at 15:04
  • Thanks, now I understand how the **set /p** works thanks a lot. – Aidan Jul 08 '18 at 15:06
  • Okay I wont delete it. – Aidan Jul 08 '18 at 15:07
  • Please read [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) There is everything explained how to offer a menu from which a user can put an option or how to give the user the freedom to enter a string and safely process it further. – Mofi Jul 08 '18 at 15:36

1 Answers1

0

Here is a basic script that will allow the user to input the IP they wish to PING. After the script runs, it will pause then loop back to user input again. Furthermore, the script will detect if a host exist's or not, prompt the user, then loop back to user input.

The command your looking for is SET with a syntax of SET [variable=[string]] The gate /P is Prompt For Input. This will allow the script to "ask" you something then can prompt it via the %variable%.

@ECHO OFF

:PING
cls & set /p adress=Type an adress you wish to ping: 
cls

ping %adress% & echo. & pause. & goto :PING
John Kens
  • 1,615
  • 2
  • 10
  • 28
  • That is a working batch file as long as the batch file user really enters what this batch code expects from user input. But it is very unsafe and could also do unexpected things even on apparently correct entered strings like `216.072.213.228`. Why all those `&` operators instead of writing one command per command line? And I suggest to read DosTips forum topic [ECHO. FAILS to give text or blank line - Instead use ECHO/](https://www.dostips.com/forum/viewtopic.php?f=3&t=774) and use in future `echo/` or `echo(` instead of `echo.` to output an empty line. – Mofi Jul 08 '18 at 20:03