Hey, I'm a complete noob to batch commands. I just found that they might be somewhat useful for some stuff I'm doing, so I thought I'd try them out.

Anyway, I've got a batch file that calls a program called Poweroff to turn on a pc on my network via WOL (wake on lan).

Here is the code:

CLS
@ECHO ON
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx
call wait 90
CLS
EXIT

As you can see, I'm calling a script called wait.bat, that simply pauses the script for 90 seconds to make sure the box has come out of hibernate before I continue processing commands. But I would like it to be more efficient. Rather than just waiting 90 seconds, and then continuing on, I would like to ping 192.168.0.3, and wait till there is a reply, and then go on. That way I can make sure the box is up before continuing.

Anyone know how to check the ping reply to make sure it is getting a reply before continuing on?
Thanks.

edit: I replaced the MAC address for security purposes.

Recommended Answers

All 29 Replies

*BUMP*
No ideas? Surely it's a simple solution. I've googled it to death but can't find anything. A batch command that could confirm whether a ping comes back successfully would be fairly useful, no? Say:

PING 192.168.0.3
IF Reply {
do this
}
else {
do this
}

But how do I check for the reply, and pass it to the IF statement?

Member Avatar for iamthwee

>Surely it's a simple solution

I don't think so, I could only think of a way to get the status back using code i.e getProcessState. Unless someone knows better?

Well thanks for the reply anyway. I think I might have something figured out. As soon as I do, I'll update this thread.
Thanks.

If you do a ping -t 192.168....

Then it will continue pinging until you hit CTL C - you will therefore see when it comes up and continue when you are ready.

If you do a ping -t 192.168....

Then it will continue pinging until you hit CTL C - you will therefore see when it comes up and continue when you are ready.

Yeah, that's great, except you can't do that in a batch script. Thanks for the idea though. I haven't had time to look further into this yet, but as soon as I do, I'll let you know how it goes.

i suppose you could do it as a script instead of a batch file

Yeah, that's great, except you can't do that in a batch script. Thanks for the idea though. I haven't had time to look further into this yet, but as soon as I do, I'll let you know how it goes.

Sorry I thought you saw it run, so you could cancel it

That's ok. Yeah, if and when I do it, it will be going in the middle of the night. But as of right now, I've kind of had to put it on hold.

As it will run in middle of night, if it always works with 90 seconds delay then I would go with that.

Just had a thought - when it is up and running can you test for the exstance of a file (have a file used as a marker file that is never deleted) If so you just keep testing for it. Make sure you have an 8.3 name for it.

I have worked it for you, here are the steps

1) do this once Ping > basefile.txt

This puts results of ping in a file - must be ping that works.

2) then batch file should be

echo off
:start
ping > newfile.txt

fc newfile.txt basefile.txt
if errorlevel 1 goto start
if errorlevel 0 goto nextstep


:nextstep

...... whatever you want to do next when connected

Just a little trick that I picked up from PERL:

|| first or second -----> Do second if first fails

&& first and second --> Do second only if first succeeds

This is a one liner that I came up with to ping a whole subnet. Very quick and dirty so not as nice as it should be:

for /%g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g

Remember for statements on the command line only use one %G when dereferencing the variable that is iterated. In a batch file or command script, you must use two percent signs, .i.e., %%G.

Whoops! I messed up the for statement. It should read:

for /f %g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g

Sorry. =8(

Whoops! I messed up the for statement. It should read:

for /f %g in (1,1,254) do @ping 192.168.20.%g 2>&1 > nul && echo Alive 192.168.20.%g || echo Dead 192.168.20.%g

Sorry. =8(

Hi

Would you explain how this works please, I thought I knew DOS commands quite well.

I have tried this from command prompt on and XP machine and I get 'The system cannot find the file 1.'

Ta

Denis

Its a perl script. It needs to be interpreted

Yeah I looked at what I wrote again. FOR /L iterates through a series of numbers using (START,STEP,END) for the parameters.

FOR /F uses the content of (FILENAME or TEXT) as its parameters.

I should have used the /L switch. Try that and it should work.

I just reread the OP and think that this might do what you need to get done:

@echo off
cls
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx

:return
ping 192.168.0.3 > nul || goto wait

cls
exit

:wait
:: Simulates ~60 second wait
:: Credit goes to www.ss64.com/nt/sleep.html

ping -n 61 127.0.0.1 > nul
:: return
goto return

Becareful with GOTOs as they are really bad way to get a loop done. But, eh.

Anyway. I would definitely test this prior to using this in a production machine.

I just reread the OP and think that this might do what you need to get done:

@echo off
cls
poweroff wol -ip 192.168.0.3 -subnet 255.255.255.0 -mac 0xxxx00xxxxx

:return
ping 192.168.0.3 > nul || goto wait

cls
exit

:wait
:: Simulates ~60 second wait
:: Credit goes to www.ss64.com/nt/sleep.html

ping -n 61 127.0.0.1 > nul
:: return
goto return

Becareful with GOTOs as they are really bad way to get a loop done. But, eh.

Anyway. I would definitely test this prior to using this in a production machine.

Thanks, I'll give this a try.

ok hi there i think i figered out how to do it
but it's a litle dificult and i need to look up
some codes but heres the trick:

you make a file of a succesfull ping
wich show howmany paackarived at adres sucesfull there should be 4 if
not mistaken

now u save this in a filed colled "good.txt"

then u u make a batch file with a ping that saves it to a txt file called "ping.txt"
and call this .bat file "ping.bat"

then u make a last and finall .bat called "your desired name here.bat" and let the script
call ping.bat
and compare ping.txt with good.txt
if there the same let it display succes
if it is falls let it display failure

this is yust in short but i'll work out the code and explain it in a tuturial here if u guys would like that

ok hi there i think i figered out how to do it
but it's a litle dificult and i need to look up
some codes but heres the trick:

you make a file of a succesfull ping
wich show howmany paackarived at adres sucesfull there should be 4 if
not mistaken

now u save this in a filed colled "good.txt"

then u u make a batch file with a ping that saves it to a txt file called "ping.txt"
and call this .bat file "ping.bat"

then u make a last and finall .bat called "your desired name here.bat" and let the script
call ping.bat
and compare ping.txt with good.txt
if there the same let it display succes
if it is falls let it display failure

this is yust in short but i'll work out the code and explain it in a tuturial here if u guys would like that

Pardon me - this is exactly what is in my post 22 days ago

well i never red beyond page one so dont get your self in a notch and it5's not like your the only one that can think of somthink like this right i meen were yust re inveting the weeel i'm sure that thyere is some one that had the same thought before u did have it to so dont make agusastion i'm yust trying to help i dont need to steal your posts + if i wouyld use ur post your credits will be there are they there ??? thought so not ur post yust me typing fast trying to help out some one from who i thought that was stuck but hey here u go mate 3 hoera'ss for dennis o somthing p.s this is a litle difrent from what u spoke about yust scrolled back read befgore u weeep hehe

ps i know my gramer is bad sorry for thaty dont have much time please meal me regarding this isue i asure u i never red ur post. before i wrote mine

My apologies hwit if I upset you.

na thats not it but if u think about it a .bat has limeted amount of commands it would be inposeble to be origenall. btw do have anny id how to start a .bat from flash project i hear alot about f_command but it doesnt work on my project and cant seem to find the problem :) myeyes hurt from all the reading that i have dne lol

myeyes hurt from all the reading that i have dne lol

And my brain hurts trying to figure out what you say with all the contractions and misspellings in your posts. :icon_twisted:

Oh well, we all don't speak English natively. But please stop using contractions like "O I C" so the non-English speakers can try to follow you better. And proofread before you submit. There's no reason to post a word like "it5's"

yea i know what u mean. like i sad before i was in a hurry and sorry for the BAD grammer. :)

but hey we all have those days right were u have to run from customer to customer and yust want
to post that post between lines of your agenda.

But i have looked around some more. And descovert that it is not poseble anny more, opening .bat files true the fscommand in
Flash 8 that is.

I was thinking about using javascripts to open the .bat files.

so never mined the last post

hwit

yea i know what u mean. like i sad before i was in a hurry and sorry for the BAD grammer. :)

You obviously don't, not really. u is not you.

but hey we all have those days right were u have to run from customer to customer and yust want
to post that post between lines of your agenda.

But every post is like that. It show you are either always in a hurry, or you just don't care. So slow down, relax, and show us your better side, the side that cares. And stop playing with the web at work. It'll get you in trouble! :icon_wink:

i preferred your old avatar, walt

lol hehe it seems u figered me out :p

so tell my self?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.