38

On a daily basis I find myself in the Windows command prompt needing to access a network drive that is mapped but disconnected. I have yet to find a command that will reconnect this drive without unmapping and remapping (which leads to a password guessing game, since I don't own these computers). I would also like to be able to script this so every night the drive is reconnected if it has become disconnected somehow.

The fastest solution I currently have is to:

  1. Type "start." to open explorer,
  2. Alt-D to focus the address bar,
  3. type the drive letter I want and press enter, and wait for it to display the drive contents,
  4. then finally, close explorer and go back to the command prompt.

I know it's a minor inconvenience, but I'm often doing this through a slow VNC or PCAnywhere connection where doing anything through GUI is awful, so I'm just wondering if there's a better solution.

12 Answers12

15

Maybe try pushd \\server\share?

wfaulk
  • 6,307
10

I have to start Windows 7 before I can run the VPN program which connects me to the company network drives. Therefore my network drives don't automatically reconnect, only opening them in Windows Explorer reconnects them.

I have made a small batch file to start the VPN, reconnect the network drives, and start some applications I always uses.

In my Batch file I have the following:

REM Connect VPN here...

REM Opens an Explorer window looking at T: forcing a reconnect
Start /min explorer t:\

timeout 3 /nobreak

REM Kill all Explorer windows beginning with "T_drive" in the title
Taskkill /fi "windowtitle eq T_drive*"

REM Finish starting up here...

exit

The Taskkill /fi "windowtitle eq" command is case sensitive!

Claus Melander
  • 101
  • 1
  • 2
7

create a batch file (refreshletters.cmd) with these commands in it

(these will only work inside a batch file)

Tested on on Win7 and XP to refresh 'Disconnected' and 'Unavailable' driver letters in a console window (command line).

@echo off
net use |FIND ":" > %temp%\used.txt
FOR /F " tokens=1,2,3 delims= " %%i in (%temp%\used.txt) do (

  if %%i EQU Unavailable  (
    net use %%j %%k
    echo Activated %%j
   ) ELSE (
     if %%i EQU Disconnected (
       pushd .
       cd /d %%j
       dir . %>nul
       if NOT exist %%j\. (
          net use %%j /del /y
          net use %%j %%k
          echo Remapped %%j
          ) else (
             echo Fixed-up %%j
            )
       popd
       ) ELSE (
          echo Checked %%j
          )
     )
 )
Brian
  • 71
  • 1
  • 2
6

This is probably a stupid question, but assuming your "disconnected" drive is H:, have you tried just changing directly to the drive?

It works for me under XPSP3:

C:\>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
Disconnected H:        \\xxxxxx\wfaulk           Microsoft Windows Network
OK                     \\xxxxxxx\business        Microsoft Windows Network
The command completed successfully.


C:\>h:

H:\>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           H:        \\xxxxxx\wfaulk           Microsoft Windows Network
OK                     \\xxxxxxx\business        Microsoft Windows Network
The command completed successfully.
wfaulk
  • 6,307
4

I know you said "net use" didnt work for you - but here is what you could try.

Run a batch file with net use /DELETE option to remove the existing/persistent connections And then you could use net use with the /SAVECRED option to re-use the passowd credentials used int eh last succesful logon. I am not sure if this will address your problem but its worth looking into.

2

I have the same problem with multiple connections, and it's intermittent. When I reboot the computer, sometimes the connection is "good" and sometimes not. I don't have the password problem since I'm on a company LAN, but still I have the other aspect of the problem.

I've created a batch file called ex.bat, and stored it (along with many other things) in a \util directory that I put in my path. I can type "ex h:" and it will run Windows Explorer and set it to go directly to the h: drive. Here is the batch file:

start explorer /e,/select,%1

This works, with a lot fewer keystrokes. Or if it doesn't work at first, hit the Enter key to open the h: root folder and display the contents. Or, type "ex h:\sub" where the subfolder "sub" exists.

This problem seems to have gotten a lot worse recently, perhaps due to one of the Windows XP security patches.

Another thing to try is on the server side. On Windows servers, modifying a Registry key is supposed to keep the server from disconnecting its connections on a timeout. I do this on pretty much all our servers. The file is:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters]
"autodisconnect"=dword:ffffffff
Gareth
  • 19,080
Warren
  • 21
  • 1
1

I had a similar problem and discovered I could force a reconnection with a simple "dir x:" from the command line (where X: was the mapped but disconnected drive.) This was from a Server 2003 system to mapped drives on XP and Win7 computers.

I created a batch file, wake_network_drives.bat, with these two lines:

    dir x:
    dir z:

and set a scheduled task to run it nightly before the backup program that was failing because the drives were occasionally showing as disconnected.

StanZ
  • 11
1

Just type:

NET USE H: \\server\share

(where H: is your drive letter of course)

Type 'NET /? USE' to see all options this command gives you.

Zoran
  • 1,079
0

The answer from StanZ worked for me, but I had to be able to programatically determine any mapped drives without a status of OK, as users in our domain get different drive mappings. I coded here to run as a stand alone version, which executes net use before and after the actual code, so that you can see the results, but you only need to copy the one FOR statement (three lines) into your code to get the desired results.

@ECHO OFF
::  
:: revision of answer from StanZ on superuser.com at the following url  
:: https://superuser.com/questions/52595/can-a-mapped-network-drive-be-reconnected-from-the-command-line/367910#367910  
:: https://superuser.com/a/367910  
:: which resulted in my  
::  
:: DriveStatusOK.bat  
:: iCharlieC  
::  
SETLOCAL ENABLEEXTENSIONS  
::  
:: you need only copy the one FOR statement (three lines)  
::   to use in your own code  
::   to force status of mapped network drives to OK  
::  
:: "tokens=1-2 delims=: " is used in the for statement  
:: to output only the drive letter from the output of...  
:: net use  
:: is piped to the first findstr which outputs only lines with drive letters  
::   then piped to another findstr to eliminate drives with a status of OK  
::   then piped to another findstr to get lines that start with a status  
::     -in other words, those lines that the first character is a letter-  
::     -this due to the fact that in our domain we have a drive  
::      that is mapped to a letter, but never has a status.  
::      I do not know the reason for this,  
::      but adding that last findstr prevents me from getting an error  
::      from trying to execute a dir on a non-existant drive letter.-  
::   then dir is executed on each drive letter (after appending ":\" to the letter)  
::      and the output redirected to NUL  
::  
:: run net use before as a visual here  
net use  
::  
FOR /F "tokens=1-2 delims=: " %%i in (  
    'net use ^| findstr ":" ^| findstr /i /v "^OK" ^| findstr /i "^[A-Z]"'  
    ) do dir "%%j:" > NUL  
::  
:: run net use after "waking" drive(s)  
net use  
::  
ENDLOCAL  
fixer1234
  • 28,064
0

Maybe an old question but still relevant in 2017 and Windows 10 because network shares are often not reconnected or attached corretly by windows for different reasons. Based on this problem and the absence of a real praticable and full working solution I created a Github project which contains a Scheduler Task and a VBS which solves the issue by simply trying to reconnect drives after resume or logon if not connected or online.

The script is full configureable and can be set up depending on the needs of the actual user.

Path to the Github project

thex
  • 311
  • 3
  • 8
0

If you aren't opposed to keeping these passwords in plain text in a batch you can script a delete and remap including credentials.

net use H: /delete
net use H: \\server\share /user:domain\user password /persistent:yes

I know it doesn't meet your requirement of not deleting the share, but it resolves the root of the problem of having to remember passwords.

MDMarra
  • 20,746
0

TL;DR summary: use net use and if exist

I ran into a related problem while creating a nightly backup script. It had to check if a computer was turned on (using ping), send a magic packet to turn it on if needed (using MC-WOL), wait for Windows to boot, then establish a connection to an SMB share and wait for it to be present.

While other answers here are great, I didn't want mapped drive letters popping in / out of existence, or to disconnect anything the user might be working with, nor to have Explorer windows flashing around. i.e. A goal was zero user distraction.

Here's a chunk of code that may be helpful to you:

set TARGET_IP=192.168.1.3
set TARGET_PATH=\\%TARGET_IP%\d$\backups
set ATTEMPTS=0

:LOOP

REM Give up eventually
if %ATTEMPTS% GEQ 200 goto NOJOY

REM Wait for IP connectivity to host, and/or burn up a couple seconds before retry
ping -n 1 -w 2000 %TARGET_IP% | find "TTL" > nul
if errorlevel 1 set /a ATTEMPTS+=1 & goto LOOP

REM See if we're already connected, and try to give it a kick
if exist %TARGET_PATH% goto CONNECTED

REM Try to reconnect
net use %TARGET_PATH% > nul
if errorlevel 1 set /a ATTEMPTS+=1 & goto LOOP

:CONNECTED
echo Connected
exit /b

:NOJOY
echo Couldn't connect

If you prefer, you can replace the IP address above with a host name (e.g. server1).

net use is needed in case no existing connection is present. I ran into some corner cases where it alone seemed insufficient to "wake up" the share (e.g. if there was a prior connection existing in a disconnected state). I found the if exists directive has a similar effect as the alternatives mentioned in other answers (like pushd, dir, Explorer, etc), but it seemed a lighter-weight approach.

rkagerer
  • 246