View Full Version : How do I write a clever batch file?
RossyP
03-10-2007, 12:15 AM
Hi all,
I'm trying to write a batch file in WinXP that will execute a few programs on startup if a net connection is detected, specifically:
if (network is on)
{
run thunderbird
run bitlord
}
Any ideas how to do this? Any help would be appreciated. I've looked into some basic batch files, the running programs is easy, its just detecting the network that I'm struggling with.
Thanks very much,
Ross.
oracleguy
03-10-2007, 12:23 AM
You might want to use a VBScript file instead, you'll have a lot more power and it'd make something like that a lot more feasable.
RossyP
03-10-2007, 12:25 AM
Ok, are you saying there's no way I could do this with batch files then? I've used a few programming languages, but I don't have the first clue about something like VB. Where could I get a compiler for it?
oracleguy
03-10-2007, 05:58 AM
You don't need one, the Windows Scripting Host is part of Windows. All you need to do is name your file .vbs and it will run it.
It might be possible with a batch file, just more difficult. The hardest part is determining if the network connection is there via a batch file.
RossyP
03-10-2007, 02:52 PM
Ok I'll look into using the VB scripting. Thanks for all your help OracleGuy.
Roelf
03-10-2007, 04:25 PM
a way of detecting internet connection is ping google.com and check the return from the command to see if a reply has been given.
rather easy from a batch file
RossyP
03-10-2007, 04:56 PM
Thanks Roelf. I can create a batch file that pings google and send the stream to a txt file. How do I feed a positive response from the ping command into an if statement for my original file?
Sorry if I'm asking stupid questions, I'm a little in over my head but I'd love to get this working. Thanks for all your help,
Ross.
sage45
03-11-2007, 09:08 AM
You wouldn't be able to feed a response into your file per se... What you would have to do is feed the ping response to a temporary file, parse the temporary file to locate the text that shows a valid/invalid response and based upon the response you could then set an environment variable that could be used by your batch file to determine the next logical step...
With a VB script this is much easier to accomplish as you use the WMI to determine a network connection. The WMI allows for better testing of a network connection, whereas anyone who modifies the hosts file of the local operating system can invalidate your batch by simply setting the hosts file with a pingable ip address for google.com (i.e. - 127.0.0.1) and in this sense, your ping test would not be a valid way to determine network connectivity.
-saige-
ghell
03-13-2007, 07:35 PM
or find or findstr could probably be piped to with the ping result to check if the response contained no "Reply from"s instead of using a temporary file.. probably.
for example
ping -n 4 google.com | find /C "Reply from"
(if all 4 pings are successful, that will echo out a 4, as long as it doesn't return 0 then at least one was successful.)
Alternatively ipconfig may be used instead for a faster but less reliable solution.
RossyP
03-15-2007, 12:41 AM
Thanks for the reply ghell. I tried running the line you gave, works well thank you. My one problem is that I can't figure out how to set it as a variable that can be checked in my batch file. I tried to set it with the name var, I wrote this:
set var= ping -n 4 google.com | find /C "Reply from"
But all it returned was what had been stored in var the time before(previously I had run it set to 123 and that was what I got). Is there a better way to set it?
You guys have been really helpful, I really appreciate it. Thanks for all your time, I know I'm making life hard but I would love to see this work.
Thanks guys,
Ross
Xiong Chiamiov
03-15-2007, 04:36 AM
Not being helpful here at all, but why are you using bitlord? I've heard lots of nasty things about it. I personally love Azureus, but utorrent, abc, etc. are good.
RossyP
03-15-2007, 11:05 AM
Good Question. to be honest, theres no real reason, it works for me and I've not had many problems with it. Heard of Azerus and utorrent, both seem good from what i hear, not heard of abc. I'm not sure it really makes all that much difference, if I ever started noticing problems with it I'd probably switch though.
maniacal
03-17-2007, 02:37 PM
I wanted to use ping.exe to see if my laptop (on my LAN) was on and available, and call it from a batch script. I hoped to find the easy way out and get code on the internet, but I had no luck, so I made my own. Well along the way, I found this thread, so I figured I'd sign up and share what I came up with:
@echo off & cls & setlocal
set Host=dv8000t
set PingParms=-n 1 -w 50
echo. & echo Pinging %Host%, please wait... & echo.
for /f %%a in ('ping %PingParms% %Host%') do (call :CheckResult %%a)
:CheckResult
if "%1"=="Request" cls & echo. & echo Host %Host% did not respond. & echo.
if "%1"=="Reply" cls & echo. & echo Host %Host% is available. & echo.
:: End of script
Some of the stuff is not strictly necessary (i.e. the setlocal, the extra echo. commands that simply add blank lines, etc.), and are just the way I like to do things. Obviously your Host and PingParms will differ, but I made it super-easy to change.
From your request, it seems as if the main thing you need to do is replace the echo commands in the last two if statements with start "" run <application.exe>. Actually, if you only want to run the application if the ping succeeds (i.e. if the host responds to the ping), you only need the if "%1"=="Reply" line.
Yes, this is somewhat of a kludge, since the %%a variable will be filled with every word the ping.exe command responds with. Also, there's not much point in using anything other than -n 1, and in fact you shouldn't use anything other than -n 1 in this script, since the if line(s) will be run for each ping result. Hey, I didn't say it was perfect... I wrote it for something other than what you want it for. I'm sure there are better-suited scripts out there, but HTH...
Oh BTW, BitLord is highly frowned upon in certain BitTorrent communities. Some private trackers even block it. I guess it's quite abusive to the tracker and to other peers.
ghell
03-17-2007, 06:50 PM
Some of the big torrent clients is banned from some private trackers because of things such as upload capping and features that allow people to leech very easily etc, I don't know if bitlord is one of these. Azureus is a big memory hog and can take a while to load on older machines, but at least its cross platform and if you use linux you are pretty much limited to azureus, command line torrent clients or a tiny torrent client such as the original bittorrent client which provides very limited information about the connections etc.
Now that I think about it, I'm not entirely sure how to capture an application's output into a variable in windows batch (something like $(...) in linux I suppose) does anyone know?
rpgfan3233
03-22-2007, 12:21 AM
KTorrent isn't too bad if you are running KDE, especially since it works with your preferences. Very simple, yet useful. I use Azureus on Windows because nothing else works as well.
ghell
03-22-2007, 12:59 PM
I used to use Azureus on windows, now I use µTorrent because it's less of a resource hog and it has a pretty graph (azureus' swarm diagram is nice too :P)
RossyP
03-24-2007, 03:15 PM
Thanks all for your comments, I didn't realise BitLord was so frowned upon. I think I'll probably switch to µtorrent, I've heard good things about that and I'm running it on an oldish laptop so something thats not such a resource hog is good. I haven't had the chance to test out the code maniacal has left(last year of my degree, dissertation work is piling up), but I'll give it a go in the next couple of days and get back to you. It looks as though it solves a similar problem to what I had, I'm hoping to sort this soon. Thanks again to all of you, your help, support and advice is greatly appreciated and very encouraging.
paqmanbiker
04-04-2007, 01:09 PM
Hey, I've just been reading through this thread, and I'm trying to do something similar to the OP. I've got a box that is being automatically turned on via the WOL (wake on lan) command, and I need this little batch script to ping the box every few seconds to make sure it is up before continuing on.
I've got the script that maniacal provided working just great to see if the box is up or not, but what I need it to do is to keep pinging the box at regular intervals until it receives a reply, then continue on with the script.
I'm just not sure how to get it to keep retrying the ping until it gets a reply. Like I said, I've got it working just how maniacal has it. Just need it to go a little further. Anyone know how to do that?
ghell
04-04-2007, 04:25 PM
You can probably use labels and goto to keep looping and jump out of the loop once a condition is met.
However, be careful because a pingable box doesn't mean that any of its services have necessarily loaded. For example if the box had a webserver on it (just as an example) you will be able to ping it before being able to request http from it.
paqmanbiker
04-05-2007, 01:09 AM
You can probably use labels and goto to keep looping and jump out of the loop once a condition is met.
However, be careful because a pingable box doesn't mean that any of its services have necessarily loaded. For example if the box had a webserver on it (just as an example) you will be able to ping it before being able to request http from it.
True, good point, I'll be sure to keep that in mind. Thanks for the idea. I tried doing the goto using a label and haven't been able to get it working yet, but I'll keep playing. When I get back to work (where my script is), I'll post what I've got.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.