Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Execute Batch File on Connection to Network


(!)

TheRobatron's Avatar
TheRobatron TheRobatron is offline
Computer Specs
Senior Member with 551 posts.
THREAD STARTER
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
22-Nov-2008, 10:24 AM #1
Execute Batch File on Connection to Network
On my network I have NAS that requires a username and password to access it, and Windows doen't seem to remember the login so I have to enter it each time I start my computer. I wrote a batch file to connect for me to save time and effort (it uses 3 network drives):
Code:
net use S: \\192.168.1.235\mainshare /user:mainshare\guest ******
net use M: \\192.168.1.235\music /user:mainshare\guest ******
net use Z: \\192.168.1.235\public /user:mainshare\guest ******
This works fine on my desktop PC but on my laptop, which uses wireless, it fails at startup because the wireless takes a while and often fails so I have to do it manually. So I'm wondering if there's a way to have the batch file running in the background and then execute that code when it detects a connection to the network (or internet). Windows Live Messenger automatically signs in when wireless connects, so I know it's possible.

TIA
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
28-Nov-2008, 03:51 AM #2
This sends one ping to the server and waits 1 second for a reply. If none received, it pings again. The server has to be set to respond to pings of course. If that's not possible, you can always ping another system, or an Internet Address. If your Internet Connection is down, it will never map the drives though.
Code:
@Echo Off
:CheckNet
ping -n 1 -w 1000 192.168.1.235 |find /I "Reply"
If %Errorlevel%==1 goto CheckNet
net use S: \\192.168.1.235\mainshare /user:mainshare\guest ******
net use M: \\192.168.1.235\music /user:mainshare\guest ******
net use Z: \\192.168.1.235\public /user:mainshare\guest ******
HTH

Jerry
TheRobatron's Avatar
TheRobatron TheRobatron is offline
Computer Specs
Senior Member with 551 posts.
THREAD STARTER
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
28-Nov-2008, 01:22 PM #3
Thanks very much for your reply

It works great (I changed it so it checked google instead) but is there a way to stop the command window opening or having it displaying all the "command failed" messages while it's waiting?
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
28-Nov-2008, 04:48 PM #4
Can't keep a Command Prompt from opening, at least not without a 3rd party app like Hidden Start.
All you can do with Windows alone is have it run minimized. If you are running this from a shortcut in the Startup folder, just set the shortcut properties Run box to minimized. If starting from the registry, start the shortcut instead of the batch file.

I don't get any "command failed" messages on mine, but I don't have a wireless card. I also only tested this on XP, not sure if Vista would behave differently.
Have only tested with the wired NIC disabled, and booting with the network cable disconnected, so it can't get an IP address (Auto IP disabled, so card shows 0.0.0.0 for it's IP, not a 169.254.X.X IP). I'm starting the batch from the Startup folder, so it doesn't run until someone logs on; not even sure if you could map drives before anyone logs on.

Is the error more specific? Most normal errors won't be displayed as they are piped to the Find command, like the following:
Destination Host Unreachable
Ping request could not find host Hostname. Please check the name and try again.
Bad value for option -X, valid range is from Y to Z.

None of these should appear, at least on XP.

You might want to comment out the @echo off statement so you can see just what command is failing, just to verify it is the Ping statement, and is not due to something else.

If it is the Ping command failing because the wireless network isn't ready yet, then add 2>Nul at the end of the Ping statement like this:
ping -n 1 -w 1000 www.google.com |find /I "Reply"2>Nul

That should stop the errors from being displayed.

HTH

Jerry
Codiah's Avatar
Codiah Codiah is offline Codiah has a Profile Picture
Computer Specs
Member with 116 posts.
 
Join Date: Sep 2008
Location: Central Texas
Experience: Intermediate
29-Nov-2008, 03:03 AM #5
You could start it from a small VBScript to hide the window....
But VBScript will not execute at startup, so if your attempting to start it from startup, you can compile a small C++ program to do the same..

here are the example below...

::::::::::::::::::::::::::::::::::::::::::::::::::::::::


VBScript
Code:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "MyFile.bat" & Chr(34), 0
Set WshShell = Nothing
::::::::::::::::::::::::::::::::::::::::::::::::::::::::


C++
Code:
#define _WIN32_WINNT 0x0500
#include <windows.h> 
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    HWND hWnd = GetConsoleWindow();
    ShowWindow( hWnd, SW_HIDE ); 
                system("CALL MyFile.bat");
                system("EXIT");
    return 0;
}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::


hope that helps....
TheRobatron's Avatar
TheRobatron TheRobatron is offline
Computer Specs
Senior Member with 551 posts.
THREAD STARTER
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
10-Dec-2008, 05:59 PM #6
Thanks very much both of you for your replies

Unfortunately I've been very busy lately so I haven't been on TSG much so sorry for the delay. I'll try your suggestions as soon as I can
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑