Computing.Net > Forums > Programming > Batch file help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file help

Reply to Message Icon

Name: way_2_hot_a
Date: August 11, 2009 at 09:59:07 Pacific
OS: Windows XP
Product: Microsoft Windows 2003 server - 5 users
Subcategory: Batch
Comment:

Im not quit sure whats wrong with this code. please help.. its supposed to ping google and if no reply it supposed to exit the internet connection "pppoe" then restart the connection again.. Need this due to unreliable internet connection. thanks

@ECHO OFF
:Game
PING www.google.com |FIND "Reply from " > NUL
IF NOT ERRORLEVEL 1 ECHO Internet connection is reciving packets boss.
IF ERRORLEVEL 1 rasdial dsl /disconnect
GOTO:protocol
:protocol 
PING www.google.com
cd "%ProgramFiles%\DSL" && START dsl"
GOTO:Game




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 11, 2009 at 22:54:02 Pacific
Reply:

How about a hint. What is it doing? Or saying?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: way_2_hot_a
Date: August 12, 2009 at 00:56:09 Pacific
Reply:

Hing ok.. ehehe.

The batch file is supposed to run constantly in my task bar pining www.google.com When google doesn't reply its supposed to end my current connection because the connection has dropped. After ending the connection the batch file is supposed to Restart the connection (happens to be a pppoe connection)

THanks


0

Response Number 3
Name: Razor2.3
Date: August 13, 2009 at 15:39:25 Pacific
Reply:

. . . And what is it doing?


0

Response Number 4
Name: xterm11
Date: August 13, 2009 at 21:09:53 Pacific
Reply:

goto:protocol should be:
goto protocol

same with goto:game

Not quite clueless


0

Response Number 5
Name: Razor2.3
Date: August 14, 2009 at 05:16:04 Pacific
Reply:

xterm11: It's bad form, but XP's CMD parses it just fine, so it's not the cause.


0

Related Posts

See More



Response Number 6
Name: xterm11
Date: August 14, 2009 at 05:36:44 Pacific
Reply:

Put pauses after each line, and see where it fails.

There are no stupid questions, just stupid people.


0

Response Number 7
Name: xterm11
Date: August 14, 2009 at 07:28:44 Pacific
Reply:

Get rid of "|FIND "Reply from " > NUL" in the ping line. You can
check the errorlevel after pinging google, to see if it succeeded.

Like:

@echo off
ping google.com
if %errorlevel% neq 0 echo ERROR! && pause

There are no stupid questions, just stupid people.


0

Response Number 8
Name: Judago
Date: August 14, 2009 at 23:12:18 Pacific
Reply:

Perhaps you don't want to start dsl(whatever that is) if the connection is active?

IF NOT ERRORLEVEL 1 (
    ECHO Internet connection is reciving packets boss.
    goto game
)

The goto :protocall seems to be worthless unless this is a condensed version of the code.


0

Response Number 9
Name: Mechanix2Go
Date: August 15, 2009 at 00:59:25 Pacific
Reply:

"Get rid of "|FIND "Reply from " > NUL" in the ping line. You can
check the errorlevel after pinging google, to see if it succeeded."

Not quite.

As yet, we don't know what the malfunction is.

While we're at it, not much point in GOingTO the very next line. LOL

"GOTO:protocol
:protocol"


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 10
Name: way_2_hot_a
Date: August 15, 2009 at 06:40:18 Pacific
Reply:

Hey thanks guys for all your help.. I figured this out by incorporating the rasdial function instead of having it run a shortcut.

here is what i came up with


@ECHO OFF
Color 0A
:Game
PING www.google.com |FIND "Reply from " > NUL
IF NOT ERRORLEVEL 1 ECHO Internet connection is reciving packets Connection functional!
IF ERRORLEVEL 1 ECHO PACKETS NOT BEING RECIEVED & rasdial dsl /disconnect & rasdial Connection name Username Password
GOTO:Game


This works perfectly. If you could help me with one more thing. I would like to make this faster. I don't know how because it waits for the ping reply. If you know another method of checking the status of the connection, and if the batch finds The connection has dropped it would disconnect the connection and reconnect it again. I Just need to speed up the process of it detecting no connection so it can get my connection back up and running faster.

THanks for any help guys.

ALex


0

Response Number 11
Name: xterm11
Date: August 15, 2009 at 23:02:48 Pacific
Reply:

ping google.com -n 1 -l 1 -w 100
Will ping only once, with only 1 byte of data ,with a timeout of 1/10 of a second if it fails (making it quicker to find if there was an error; change it to be slower or faster depending on your connection speed) which should make
it much quicker.

What I was saying m2go, is that he doesn't need to find "reply from", he just needs to find if there was an error after attempting to ping google. This works perfectly, I tested it.

@echo off

:ping
ping google.com -n 1 -l 1 -w 100
if %errorlevel% equ 1 (echo Disconnected && pause) else (echo Connected)
goto ping

There are no stupid questions, just stupid people.


0

Response Number 12
Name: way_2_hot_a
Date: August 16, 2009 at 21:28:53 Pacific
Reply:

WOW thank you soo much xterm11 so much help my friend.
This works perfectly!


0

Response Number 13
Name: way_2_hot_a
Date: August 16, 2009 at 21:38:38 Pacific
Reply:

excuse me sir. if you are familiar with the rasdial command. Usually you use the rasdial command with the connection name, user-name, password fallowing the command. i was wondering if there is a way for the batch file to receive the required details (connection name, user-name, password) from a txt file that's in the same folder?


THank you


0

Response Number 14
Name: xterm11
Date: August 16, 2009 at 21:53:12 Pacific
Reply:

The text file must be in the same folder as the batch file, and
the information should be as follows:
Connection name
username
password
Save it as rasdial.txt

Here is the script


for /F "tokens=* delims=<enter>" %%a in (%~dp0\rasdial.txt)
do (
set skip=f
for %%b in ("cn" "un" "pw") do (
if "!skip!" == "f" (
if not defined %%~b (
set skip=t
set %%~b=%%a))))

**ALL THE OTHER CODE HERE**

IMPORTANT
The rasdial command should look like:
rasdial %cn% %un% %pw%

There are no stupid questions, just stupid people.


0

Response Number 15
Name: way_2_hot_a
Date: August 16, 2009 at 22:13:08 Pacific
Reply:

Sorry sir but i don't quite understand how to incorporate your code into my batch

Here is my code if you would be so kind to help me understand how to incorporate your code into mine


@ECHO OFF
Color 0A
:Game
PING www.google.com |FIND "Reply from " > NUL
IF NOT ERRORLEVEL 1 ECHO Internet connection is reciving packets Connection functional!
IF ERRORLEVEL 1 ECHO PACKETS NOT BEING RECIEVED & rasdial dsl /disconnect & rasdial ConnectionName UserName Password
GOTO:Game

I am also in the process of incorporating your tip that u explained above about the 1 byte ping with a short timeout.


Thanks so much. You have already helped a ton


ALex


0

Response Number 16
Name: xterm11
Date: August 16, 2009 at 22:34:05 Pacific
Reply:

Try this

@ECHO OFF
Color 0A

for /F "tokens=* delims=<enter>" %%a in (%~dp0\rasdial.txt)
do (
set skip=f
for %%b in ("cn" "un" "pw") do (
if "!skip!" == "f" (
if not defined %%~b (
set skip=t
set %%~b=%%a))))

:Game
ping google.com -n 1 -l 1 -w 100
IF ERRORLEVEL 0 ECHO Internet connection is reciving packets Connection functional!
IF NOT ERRORLEVEL 0 ECHO PACKETS NOT BEING RECIEVED & rasdial dsl /disconnect & rasdial %cn% %un% %pw%
GOTO:Game

There are no stupid questions, just stupid people.


0

Response Number 17
Name: way_2_hot_a
Date: August 16, 2009 at 22:56:28 Pacific
Reply:

Yes sir i tried it. The thing is it doesnt do anything. It just flashed the cmd prompt then closed again. Any ideas?


THanks again

ALex


0

Response Number 18
Name: xterm11
Date: August 16, 2009 at 23:22:11 Pacific
Reply:

@echo off
setlocal enabledelayedexpansion
for /F "tokens=* delims=<enter>" %%a in (rasdial.txt) do (
set skip=f
for %%b in ("cn" "un" "pw") do (
if "!skip!" == "f" (
if not defined %%~b (
set skip=t
set %%~b=%%a))))

:loop
ping google.com -n 1 -l 1 -w 100
if !errorlevel! equ 0 (echo Internet connection is reciving packets, Connection functional!) else (echo PACKETS NOT BEING RECIEVED && rasdial dsl /disconnect & rasdial %cn% %un% %pw%)
goto loop

There are no stupid questions, just stupid people.


0

Response Number 19
Name: way_2_hot_a
Date: August 16, 2009 at 23:45:21 Pacific
Reply:

Sorry bro for the inconvenience. Most of the code is working. I can observe when it gets to the rasdial command its not running rasdial to restart the the "pppoe" connection. Im not sure if it is ending the connection but it doesn't matter because its using the "&" NOT the "&&" here is the screen shot to help out a little bit.. thanks for helping out.
Batch file and rasdial.txt are both on desktop. thanks

http://img252.imageshack.us/img252/...


ALex


0

Response Number 20
Name: Judago
Date: August 17, 2009 at 00:42:52 Pacific
Reply:

Rename you script, naming script after a command(especially one that is used in the script) can cause many problems.


Open a cmd window an type in "set path" you should get two variables displayed(there may be more) "path" and "pathext".

A good deal of commands tend to be external (i.e they are actually .exe's, .com's, ect.), rasdial is one of them. when an external command is called first the cmd processor looks in the current directory, then each of the semicolon delimted directories are checked.

These directories are checked for the command name with the pathext file extensions, in the order they are listed as soon as it finds a match it calls it and quits.


In your case of "rasdial.bat" something like this happens.

(
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
)

rasdial.bat rasdial?
cmd: it's not built into me, I'll look for it!
cmd: First I'll start with the current directory,
cmd then go over the path directories.
cmd: Checking the current directory for:
rasdial.com...NO
rasdial.exe....NO
rasdial.bat.....YES!!!!
cmd: run rasdial.bat
cmd: I found rasdial so I'll stop looking.

Of course after which rasdial.bat asks for rasdial again, hence the setlocal recursions.


0

Response Number 21
Name: way_2_hot_a
Date: August 17, 2009 at 01:12:31 Pacific
Reply:

WOw Judago You nailed it right on the head my friend.. thanks alot for that.. had me stumped for a few hours.. U noticed it right off the top of the notepad doc. wow.. nice eye.

Now with the code that was so nicely given to me by xterm11
It is not getting the code from the txt document or its reading the txt document incorectly. hese a screen shot. If someone could kindly help me.. this is the final error
http://img300.imageshack.us/img300/...

THanks Judago and xterm11.

Any ideas?

ALex


0

Response Number 22
Name: way_2_hot_a
Date: August 17, 2009 at 01:38:11 Pacific
Reply:

OK OK i figured it out. Thanks so much xterm11 couldn't have done it without you bud. That helps so much. i thought the rasdial.txt had to be like this

connectionName
Username
Password

when i tried it like this=
connectionName Username Password << IT WORKED


Thanks alot xterm11 helped a TON


And Judago w/out you i wuda been stuck and nvr changed the name of the freakin batch file. Thanks dude!!!! IT works PERFECTLY thanks to u guys... no bugs at all thanks guys.!!


0

Response Number 23
Name: Judago
Date: August 17, 2009 at 01:48:05 Pacific
Reply:

[edit]
Posted after the problem was fixed!
[edit]


xterm11 wrote.
"Connection name
username
password
Save it as rasdial.txt
"

I'm a little bit lazier about it, it would be soooo much easier to put all of these things on one line, delimited by something like a tab, then run a for loop over it.

Here is my interpretation:

<untested>

@echo off
setlocal enabledelayedexpansion
::rasinfo.txt
:: one line
:: <connection name><tab><username><tab><password>
:: Nothing else!!
for /F "tokens=1-3 delims=	" %%a in (rasinfo.txt) do (
	set ras_connect=%%a
	set ras_user=%%b
	set ras_pass=%%c
)


:loop
> nul ping google.com -n 1
if not errorlevel 1 (
	echo Internet connection is reciving packets, Connection functional^^!
) else (
	>nul ping google.com -n 3
	if not errorlevel 1 (
		echo PACKETS NOT BEING RECIEVED 
		rasdial %ras_connect% /disconnect
		rasdial %ras_connect% %ras_user% %ras_pass%
	)
)
::The below ping it for a time out perion
:: works on xp, if it doesn't work on vista
:: use "localhost" instead.
:: a sleep function would be better....
>nul ping 0.0.0.0 -n 60
goto loop

You can change the delimiter to something other than a tab if you like....


0

Response Number 24
Name: Mechanix2Go
Date: August 17, 2009 at 02:26:58 Pacific
Reply:

Like I said about 14 replies back, you need to FIND "Reply".


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 25
Name: way_2_hot_a
Date: August 17, 2009 at 02:29:38 Pacific
Reply:

Wow thats a good looking code. Looks like its very efficient,

The problem im having with your code judago is when i simulate a disconnection "removing the telephone cord" from the modem or disconnecting the connection from task tray it doesn't reconnect the connection

screen shot: http://img39.imageshack.us/img39/61...

would be nice to get your code working for me.

Thanks


ALex


0

Response Number 26
Name: Judago
Date: August 17, 2009 at 02:41:47 Pacific
Reply:

"Like I said about 14 replies back, you need to FIND "Reply".

@M2
Isn't that just an issue with the win 2000 ping if the host refuses the ping(i.e. not an issue on the target of xp)?


The problem im having with your code judago is when i simulate a disconnection "removing the telephone cord" from the modem or disconnecting the connection from task tray it doesn't reconnect the connection.

How long did you give it? The "ping 0.0.0.0 -n 60" causes a time out for roughly 1 minute(59.044 seconds acording to ntimer on my machine) after each sucessful ping.

I tested the ping part but not the rasdial, my modem has a login client I don't really want to screw with.... Unpluging the cable caused bad pings(after the timeout elapsed) for me pluging it back a caused pings to suceed again.


0

Response Number 27
Name: way_2_hot_a
Date: August 17, 2009 at 03:45:46 Pacific
Reply:

Well i have just disconnected the connection so its impossible for a ping reply, It doesn't echo packets not being recieved, doesn't progress to rasdial /disconnect or rasdial. so after it detects no ping reply it just stops. Whats your idea?

THanks

ALex


0

Response Number 28
Name: Judago
Date: August 17, 2009 at 03:56:50 Pacific
Reply:

This line:

">nul ping 0.0.0.0 -n 60"

Was only meant to be a time out so that the script is not endlessly sucking down bytes, like I said it takes about a minute, try removing that line and try again.


M2 was completely correct and is vindicated, looking into it.


0

Response Number 29
Name: way_2_hot_a
Date: August 17, 2009 at 04:05:34 Pacific
Reply:

Yup removed that line and it sticks or stops at the same place. Erlyer i tried the same thing but i waited a good 3minutes for the delay function to resume the script but it didn't do anything either. It pings just fine but when it encounters no reply it doesn't do anything.

THanks for the reply

ALex


0

Response Number 30
Name: Judago
Date: August 17, 2009 at 04:10:12 Pacific
Reply:

M2 is completely correct and vindicated, from another thread I though that it was only about win 2000 and pinging a host that refused, I guess I eat my words....
<Still fixing....


0

Response Number 31
Name: Mechanix2Go
Date: August 17, 2009 at 04:18:49 Pacific
Reply:

Hi Judago, it may be a 2K/XP issue. No XP here to try it with.

With 2K it will return 0 as long as it resolves name to IP, regardless of a reply.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 32
Name: Judago
Date: August 17, 2009 at 04:41:49 Pacific
Reply:

Hi M2, I don't know what to to believe anymore, it would be
nice to have more consistency..... Anyhow at least it isn't
completely ignored. I came to the conclusion a while ago
when you posted the address to you server, which (if I
remember correctly) was configured to cause the issue, my
errorlevel was one so I assumed that it was a 2k issue.
Guess it doesn't hurt any way.....

I think I may found the *real* issue with this screwy batch, finding reply alone doesn't help.

This may not go for everyone but when I disconnect the phone line my modem starts replying to pings(even those addressed to google!).

Give this a whirl it *might* work this time.....

I left the timeout in but I moved it.

@echo off
setlocal enabledelayedexpansion
::rasinfo.txt
:: one line
:: <connection name><tab><username><tab><password>
:: Nothing else!!
for /F "tokens=1-3 delims=	" %%a in (rasinfo.txt) do (
	set ras_connect=%%a
	set ras_user=%%b
	set ras_pass=%%c
)
for /f "tokens=1* delims=:" %%a in ('ipconfig') do set exclude=%%b
set exclude=%exclude:~1,-1%

:loop
ping google.com -n 1|find /v "%exclude%"|>nul find /i "reply"
if not errorlevel 1 (
	echo Internet connection is reciving packets, Connection functional^^!
 	>nul ping 0.0.0.0 -n 60
) else (
	ping google.com -n 1|find /v /i "%exclude%"|>nul find /i "reply"
	if errorlevel 1 (
		echo PACKETS NOT BEING RECIEVED 
		rasdial %ras_connect% /disconnect
		rasdial %ras_connect% %ras_user% %ras_pass%
	)
)
goto loop


0

Response Number 33
Name: way_2_hot_a
Date: August 17, 2009 at 05:01:45 Pacific
Reply:

OMG wow its beautiful!! love it. Its like the Mona Lisa of batch files. hehe

THanks this is godlike. ok i set the ping delay to 1. can i get it to ping faster? If not its fine this works really well

THANK YOU SO MUCH Judago

ALex


0

Response Number 34
Name: Judago
Date: August 17, 2009 at 05:07:10 Pacific
Reply:

" ok i set the ping delay to 1. can i get it to ping faster?"

You *can* set the ping timeout slower with "-w <number in miliseconds>"(like xterm11's does) just be careful set it high enough that it doesn't timeout when you download something, I believe the default is 300 miliseconds.


0

Response Number 35
Name: way_2_hot_a
Date: August 17, 2009 at 05:41:43 Pacific
Reply:

Ok thanks Judago. i Just left it at -n 1 when i use the -w which i did before asking you how to make the delay shorter -w makes it act weird. echo's me connection is functional but sometimes it just stops"pauses" then keeps going because indeed ping replys are being received.. i can conclude 1 sec is the bare minimum for this to run smoothly.. your help Judago and the help of xterm11 is greatly appreciated. thanks you very much.


THanks again


ALex


0

Response Number 36
Name: xterm11
Date: August 17, 2009 at 05:47:14 Pacific
Reply:

Yeah dont set timeout too low, Judago has a valid point when
it comes to downloading.

M2Go, I really never knew that, I don't have any problems on
xp when checking errorlevel after pinging.

So my script I supplied is now working, correct? We're just
trying to get Judago's to work?

No matter how many times you go over a batch file, there will
almost always be an error you won't catch.

There are no stupid questions, just stupid people.


0

Response Number 37
Name: way_2_hot_a
Date: August 17, 2009 at 07:40:54 Pacific
Reply:

Yup xterm11 your batch file is working well, using it to keep my connection alive actually. hehe.. thanks you guys.. both scripts work flawlessly. Thanks loads


ALex


0

Response Number 38
Name: Mechanix2Go
Date: August 17, 2009 at 07:47:06 Pacific
Reply:

"doesn't timeout when you download something"

hmm...


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 39
Name: way_2_hot_a
Date: August 17, 2009 at 07:57:38 Pacific
Reply:

Nope torrenting full blast and have a stable connection. I changed your -w to a -n 1 so it would leave a long enough delay b4 executing the disconnection. because it was getting a time out then disconnecting the connection reconnecting it then doing that over and over.. but with the -n 2 im monitoring it. so far stable connecting for over an hour while browsing the net and torrenting. Have any other advice?

THanks

ALex


0

Response Number 40
Name: xterm11
Date: August 17, 2009 at 08:11:30 Pacific
Reply:

It's great to see an application like this for a batch file.

There are no stupid questions, just stupid people.


0

Response Number 41
Name: way_2_hot_a
Date: August 17, 2009 at 09:06:03 Pacific
Reply:

Yup thanks so much.. Helped me a ton. well a ton and a half. Coded in batch yup.

THanks


ALex


0

Sponsored Link
Ads by Google
Reply to Message Icon

Copy a file to system32 f... Script Help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file help

batch file help www.computing.net/answers/programming/batch-file-help/13830.html

Batch File Help www.computing.net/answers/programming/batch-file-help-/11464.html

batch file help www.computing.net/answers/programming/batch-file-help/190.html