Husky
Husky

Reputation: 9

How to take user input, and then run a CMD command using that input? (BATCH)

I'm making some "Private" programs using Batch, and wan't a user to input an Internet Protocal into the batch input, and then when you hit enter, it takes that input, and runs a ping command on that IP? How would one go across such thing?

~ Husky

Upvotes: 0

Views: 1763

Answers (2)

Hackoo
Hackoo

Reputation: 18847

You can give a try for this sample batch :

@echo off
Title Ping hosts tester 
:PingLoop
Color 0A & cls
echo Type the host IP to get info about its ping
set /p "IP=> "
ping %IP%
echo.
echo Hit any key to continue for another IP ...
pause>nul
Goto :PingLoop

EDIT : To check and validate the ip address entred by user before the pinging

@echo off
Title Check IP and Ping tester
:Main
cls & color 0E
echo Type your IP address :
Set /p "IP="
Call :CheckValidIP %IP%
cls
IF "%errorlevel%" EQU "0" ( 
    cls
    Color 0A
    echo %IP% is valid 
    Ping %IP%
    echo.
    echo Hit any key to continue for another IP ...
    pause>nul & Goto Main
) else (
    cls
    Color 0C
    echo %IP% is not valid
    echo Hit any key to continue for another IP ...
    pause>nul
)
Goto Main
::*********************************************************************************
:CheckValidIP <IP>
(
echo If IsValidIP("%~1"^) = True Then
echo    Wscript.Quit(0^)
echo Else
echo    Wscript.Quit(1^)
echo End If
echo Function IsValidIP(IPAddress^)
echo    Dim objRegExpr
echo    Set objRegExpr = New RegExp
echo    objRegExpr.Pattern = "\b((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)\b"
echo    objRegExpr.Global = True
echo    objRegExpr.IgnoreCase = False
echo    IsValidIP = objRegExpr.Test(IPAddress^)
echo    Set objRegExpr = Nothing
echo End Function
)>"%tmp%\%~n0.vbs"
Cscript /nologo "%tmp%\%~n0.vbs"
Exit /b
::*********************************************************************************

Upvotes: 0

geisterfurz007
geisterfurz007

Reputation: 5874

As all others allready stated in the comments you are looking for

set /p myVariable= Asking for value here

You can then use this variable enclosing it within % ->

ping %myVariable%

should do the trick. You do not neccessarily need the start cmd.exe /k but you can use it.

Feel free to ask questions!

Upvotes: 1

Related Questions