-
November 27th, 2002, 09:56 PM
#1
Junior Member
Variables in batch program.
Hello, folks...
I have a simple batch programming question. I know some people wrote tutorials about batch programming but they don't have this material.
How can I use varialbes in batch file. For example, I want to write a program such as if a server replies to ping, then outputs Alive, otherwise No. The first line is
ping x.x.x.x | find /C "Reply"
Since the ping command outputs "Reply from x.x.x.x" 4times if the server is alive, the find command above will generate a nonzero number. So, I was trying to put the number into a variable and use IF command, somthing like IF nonzero ..... echo Alive.
But how can I put the find result into a variable?
Did I make myself clear?
Thanks!
-
November 27th, 2002, 10:24 PM
#2
That example you gave should work. Say you have a bat file called blah.bat and the content is this ..
ping $1 | find /C "Reply"
you would type blah www.yahoo.com and the output would be 4 .. yahoo is responding.
if you were to type blah www.ilovetheriaa.com the output would be 0 becuse there is nothing responding. I guess I'm not understand you because it sounded like you answered your own question .. how exactly do you want the output? If you are trying to make it so it will do something when a 0 is outputted I don't know what you would do. I don't think bat file programming goes that deep.
-gunder
-
November 27th, 2002, 11:06 PM
#3
Your not looking real hard . I posted a batch file that does almost exactly what your looking for. it check to see if your connected by pinging
You can look for it if you like but basically if you forget about using vars try this
---------------
Echo . >>somename.bat
ping -n 1 whoeverthefu.com |find /C "Reply" >somename.bat
call somename.bat
del somename.bat
---------------
write a bat file called 1.bat:
---------------
cscript there.vbs
---------------
and write ‘there.vbs’ with
---------------
MsgBox "Yep its there!"
---------------
I don’t have time to test it right now it so their might be a bug but I use variation of this all the time. if i can help let me know
or you could consolidate it:
Echo cscript there.vbs >>1.bat
Echo MsgBox "Yep its there!" >>there.vbs
Echo . >>somename.bat
ping -n 1 whoeverthefu.com |find /C "Reply" >somename.bat
call somename.bat
del somename.bat
del 1.bat
del there.vbs
Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”
-
November 28th, 2002, 11:01 AM
#4
Junior Member
My question is answered by Tedob1. But I really thank both of you, gunder and Tedob1, for replying.
Tedob1, could you tell me where your previous post is? I want to study it, but I couldn't find it....sorry, maybe I am not looking real hard as you said....
And why do I need the third line : Echo . >>somename.bat ?
It seems that the batch program works without that line.
And do you know any materials or websites for cscript you mentioned above?
Thank you very much!
-
November 28th, 2002, 11:37 AM
#5
Cscript.exe is just the program which interprets the VBScript which you have in the file 'there.vbs' in that sample code.
The function of the line 'Echo . >>somename.bat' seems to be simply to create the file 'somename.bat'. The batch file should (as you have said) work fine without that line.
There is a VBScript Primer on Microsoft's website which offers a good introduction to VBScript. Not sure if that's what you were looking for though.
Hope that helped you out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|