# Execute Batch File on Connection to Network



## TheRobatron (Oct 25, 2007)

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): 

```
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 (Aug 8, 2007)

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.

```
@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 (Oct 25, 2007)

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 (Aug 8, 2007)

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 (Sep 27, 2008)

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*

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

*C++*

```
#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 (Oct 25, 2007)

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


----------

