7

I have a network drive already mapped. But I want to create a batch file that starts after the startup is complete and reconnect to the mapped network drives.

So far I have seen commands like:

net use Z: \\myserver\folder_name

But I think it maps a network drive. However I have already created a mapped network drive before. I want to connect to it.

Why I am not using reconnect at logon:

I have to connect to a VPN manually. Only after that will I be able to connect to the mapped network drive manually. Hence I am writing a batch file which connects to the mapped network drive, among other things like starting a few essential programs.

System Information:

Windows 7 Enterprise

Added Note:

In the link mentioned above "Can a mapped network drive be reconnected from the command line?" I see a somewhat working solution by Claus Melander. However the part where I am supposed to assign a title to the opened Windows Explorer window does not work. Because the opened window does not have the title I have specified.

REM Reconnect to mapped network drives
REM Y drive
REM Opens an Explorer window looking at Y: forcing a reconnect
start "Y_DRIVE" /MIN explorer Y:\
REM Wait for 5 seconds to allow it to reconnect, Ignore key presses and wait specified time during this time.
TIMEOUT /T 5 /NOBREAK
Taskkill /fi "windowtitle eq Y_DRIVE"

However, If I jump to a sub folder of the mapped drive, the statement to kill the application by filtering based on window title seems to work.

I am looking for a more elegant solution at this point.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Ayusman
  • 432
  • 1
  • 6
  • 17
  • sounds a little different from the linked question (which is about resuming a disconnected network drive) which is one of the possible states the op could be dealing with, but another is the case where the connection does not exist at all yet still needs to be handled. – Frank Thomas May 13 '13 at 16:01
  • 3
    This is NOT a duplicate question. My problem is entirely different. – Ayusman May 13 '13 at 21:21

2 Answers2

10

If I understand you correctly, you need to programmatically disconnect the existing share before connecting, but are not sure whether it will be present or not.

net use z: /DELETE
net use z: \\myserver\folder_name

will unmap drive Z if it is already mapped, and then establish a network drive to the folder_name share.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Frank Thomas
  • 35,097
  • 3
  • 77
  • 98
  • 7
    I do not want to disconnect the existing share. Because it's not connected to begin with. Just as I do it in a windows explorer window; I just double click on the window and it comes up. The solution you are trying to suggest is to remove the mapping and remap it. My point is since the mapping is already provided in the past, I should not have to re-map it, I just have to reconnect it. – Ayusman May 13 '13 at 21:01
  • 3
    @Ayusman It just doesn't work that way. A "disconnected" drive (with the red cross through it) isn't really connected AT ALL. Explorer just remembers it from a previous session and will (behind your) back call "net use" when you double-click it. To do it in a batch file you have to call "net use" yourself. That is the only way to do it. – Tonny May 13 '13 at 21:32
  • @Tony ok, I took your advise. I will have to restart my PC and see how it works. Thanks for the input. – Ayusman May 13 '13 at 21:51
  • This is the only code that worked for me. – NT. Feb 20 '15 at 12:47
3

NET USE [driveletter:] \ComputerName\ShareName /PERSISTENT:YES That will always reconnect the drive on logon.

Erik
  • 264
  • 2
  • 3
  • 2
    No it won't, not if its already defined but disconnected. – dsz Aug 21 '19 at 04:01
  • 2
    Actually, this does work -- even without the /PERSISTENT flag. It doesn't matter that it was already defined -- not on my Windows 7 Professional computer. "net use" alone showed the drive mapped but "Unavailable" until I ran this command (without "PERSISTENT", but I doubt that mattered). Result was "The command completed successfully' and the drive went from "Unavailable" to "OK" – Bob60506 Sep 22 '19 at 15:21