ms-dos if statement

April 20, 2011 at 05:41:58
Specs: Windows XP
I love writing batch files by the way : ) and I am trying to write a batch file that takes an input (the name of a computer) from the user and checks to see if that computer name is valid or exists on the domain that he/she is on then outputs/echo's if it is true (exists) or false (doesn't exist). I tried using the if exist statements but I can't seem to figure this out, how can I search for a name through a domain? Usually if exist statements only search over you computer. Is there maybe some other means of doing this, I also looked into for loops which I haven't really dealt with much but it still didn't seem to help. I can take in inputs fine and output fine it is just the whole if statements that are giving me trouble. Thanks to anyone that can help.


See More: ms-dos if statement

Report •

#1
April 20, 2011 at 15:36:15
may be it helps..
assuming all the computers are part of the same domain, might this helps.

net view ..store the result in a text file

get input from user and then search for the string in that text file..
or instead of using text file u can pipe the input as well..

net view | findstr /i "userinput"


Try it and let us know if questions

Subhash Chandra.


Report •

#2
April 28, 2011 at 09:59:55
okay that sort of helped, now maybe i'm not thinking right on this one. I understand now how i can search for the computer on the network/domain but now how can I output (echo) whether or not that computer exists? I do know how to store variables in a text file but using the "net view | findstr /i "computer"" how can i make that statement output to a variable? Am i able to just put an if statement in front of that in some way or not (i tried a bunch of different ways but couldn't get it to work)? Thanks for all the help.

Report •

#3
April 28, 2011 at 11:45:05
never mind i think i got it now. I used the following lines:

for %%i in ('"net view | findstr /i ..userinput"') do (set var=1)
if %var% == 1 (echo true) else (echo false)

But I am still having trouble with one little thing, if the "findstr" fails to find the user input then it doesn't echo false it justbreaks the code. Now that I think about it I wonder if it sets the variable equal to nul which is why it might break. Any comment on what you might think is going on or how to make this output right?


Report •

Related Solutions

#4
April 28, 2011 at 16:18:05
net view | find /i "%userinput%" >nul
if %errorlevel% == 0 (echo found) else echo Not found

Subhash Chandra.


Report •

#5
May 1, 2011 at 17:20:28
Perfect! huh, I had tried something like that but I wasn't using the errorlevel as a variable. I appreciate the help. thanks.

Report •

#6
May 6, 2011 at 12:42:05
You welcome :)

Subhash Chandra.


Report •

Ask Question