Question:
CMD command Help?
?
2017-03-09 14:17:27 UTC
hello, As a newbie to IT I was wondering if it's possible to add an IF command to a CMD Batch file. E.G IF Ipv4 Contains 192.168 THEN Start www.youtube.com.
can anyone help
Three answers:
husoski
2017-03-09 15:23:29 UTC
Yes, there is an if statement, but the manipulation of % symbols as strings is limited, and the set of things you can do with %name% variables is different from the things you can do with %1, %2, etc. arguments.



You can easily test if a variable begins with 192.168 by using a substring:



if %ipv4:~0,7% == 192.168 goto LOCAL_IP



The syntax is for substrings is %name:~offset,length%, so if you have already determined that a URL begins with HTTP://, you can see if a local IP address follows that with:



if %url:~7,7% == 192.168 goto LOCAL_URL

if /i %url:~7,9% == localhost goto LOCAL_URL



That last show the use of /i to perform a case-insensitive compare.



You can put those ideas together with looping and the call-to-a-label statement to make your own "contains" test. It's a bit of a mess, though, so you're better off trying to live without it before you go down that road.



Command line help has a lot of information. Try:

IF /? or HELP IF ... for more information about the if command

SET /? or HELP SET . . . for more information about things to do with % variables

CALL /? or HELP CALL . . . for info about arguments %1, %2, etc. and call to label
Chris
2017-03-09 18:05:52 UTC
http://lmgtfy.com/?q=cmd+if
Laurence I
2017-03-09 15:04:40 UTC
yes and no. you can write your own exe files to do comparisons. but you probably should realise that you can also run SCRIPTS. you also have redirection so you can use the > to out put to a text file or >> to append. so ipconfig/all >results.txt will hold all of the text output that you are interested in. you can then do a FIND command to see if the file contains your text. you can also feed output back into the input of another command using the <. or just use a pipe. so you can do it all in a lenghty kind of way


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...