Saturday, 7 January 2012

Could Not Reconnect All Network Drives in Windows 7

I often see the notification balloon popping up from system tray to tell me that my network drives could not reconnect. I typically see this after I've first turned on my computer or after I've booted from Hibernation.

When I open Computer, I can see my network drives, but they are shown with a red cross indicating that there is indeed a problem with the connection to them. Applications which rely on data from these network drives cannot obtain their data, and fail in their own ways. The particularly annoying aspect of this is that there absolutely no problem with the network connection to these drives. Simply double clicking on the drives will open them up and restore the connection. All applications will function correctly thereafter... until the next time I boot up the machine.

Below is a workaround you can use to ensure your remote drives are available for use when you turn on your PC. For my environment, I'm running Windows 7 64-bit and have a Synology DS411J which is a NAS drive. But really, this should work for connecting any remote network drive.

The trick to getting around this is to tell Windows to map your network drive every time your computer boots up. It turns out this is quite easy to do. All you have to do is write a script which, when executed, will reconnect your network drive and then set up a task to run the script when the computer starts up. Mapping a drive simply means that in Computer, a drive, with a drive letter - say Z: - so that whenever you refer to a location on the Z:, Windows will actually look at the network location mapped to that letter.

Creating the Script
You want to create a Windows Batch File (which has a .bat filetype) which will serve as your script. You need to know at this point which drive letter you will be using for this network drive. I use z: in the following example. You will also need to know the location on the network that you are wanting to map a drive to. In the following example, I am accessing \\DISKSTATION\Media folder which is on my NAS drive.

In the following example, I first remove the drive and then add a new one in. I do this just in case any other folder has stolen the Z: somehow. I always want to be able to depend on the Z: having my media so I find it better to remove first, then re-add.

  1. Create a new file which has a .bat file extension
  2. Give it a name (MapDrives.bat for example) and put it somewhere you'll remember.
  3. Open this file in Notepad.
  4. Add the following lines:
    • net use z: /Delete
    • net use z: \\DISKSTATION\Media
  5. Save and close the file.
  6. Double click the file and make sure your drive appears in Computer as you'd expect. If you can't see the new drive:
  • Check you can actually reach the network location. 
  • Edit the script, and add the word pause on a new line, after your other two lines. Run the script again and it should tell you if there was an error.
Your script should resemble the following:


Running the Script

Now that you have a working script, you need a way to get this script to run automatically. You could put it in your startup folder, but I prefer to use the built-in Task Scheduler.

  1. Open Task Scheduler
    • If you can't find it, enter %windir%\system32\taskschd.msc /s into your Run command.
  2. On the right hand side, under Actions, click Create Task...
  3. Name your task something sensible: Map Network Drives for example
  4. Click the Triggers tab
  5. Click New... button
  6. Add At startup
  7. Under Advanced Settings, select to delay the task for 30 seconds.
  8. Repeat to add another trigger, this time add On workstation unlock - you may wish to add a delay here too although mines is working without doing so (See the Triggers screenshot below for an example)
  9. Click the Actions tab
  10. Click the New... button
  11. Click the Browse... button and find your .bat file. (See the Action screenshot below for an example)
  12. Click OK button, then click the OK button to create the Task.
  13. Click on Task Scheduler Library from the left pane, and check that your new task is in the list of tasks. If you can't see it, right click in the task list and click Refresh.
  14. Click on your task, then click Run from the Actions pane.
  15. Check Computer, to make sure your drives are there. If you want, right click and disconnect the drive to do a proper test. Once removed, go back to Task Scheduler, and click Run again.
That is you all set up to go. Restart your computer and make sure your drives are still available to you.

Note, you might still see the balloon popping up telling you there was a problem connecting to your drives, as that 30 second delay might not have happened by that point. You can disable this balloon notification if you so choose. When you see it, click on the spanner icon, and select Hide icon and notifications.

Any problems? Not working for you? Steps missing? Let me know in the comments below!