title: Clean Up Disconnected Network Drives
body: <p>I have a app which requires a connection to a remote windows. What I have done is to use mapped drive to build the connection on Windows startup menu.  The partial codes look like this:</p>
  
  <pre><code> @IF NOT EXIST \\remotePC\sharedFolder NET USE * \\remotePC\sharedFolder /USER:user1 pwd /PERSISTENT:YES
  </code></pre>
  
  <p>The net use map works fine. The mapped drive will be one from available network drive letters.  In my app, then UNC name is used instead(\remotePC\sharedFolder).</p>
  
  <p>The issue(not sure it is an issue) is that after several reboots, I can see a list of disconnected drivers from File Explorer:</p>
  
  <p><img src="https://i.stack.imgur.com/qLXSO.jpg" alt="enter image description here"></p>
  
  <p>I am not sure all the letters would be used up if I reboot my PC many times? I would like to clean up all the disconnected network drives.  Can I do it from the batch script? I tried to use net use again for a disconnected drive, it deletes OK first time, but it stays in File Explorer.</p>
  
  <pre><code>NET USE T: /DELETE
  </code></pre>
  
  <p>Notes: I am not sure the usage of option /PERSISTENT:YES. I think it should reconnect the network drive after log in or reboot. It does not happen. That's why I added my scripts to Start up menu. I also realize that my other file explorer tools also display the disconnected network drives. Each time I reboot, one more disconnected network drive appears. It seems that the disconnected network drives does exists in Windows. Is there any way in scripts to find them and clean them?</p>
  
  <p><strong>Update</strong>: I run my test up to 10 times and now I get to the stage that no more letter available for network drive mapping. Here is the snap-shot of my network drive status by using Total Commander:</p>
  
  <p><img src="https://i.stack.imgur.com/Xrgkj.jpg" alt="enter image description here"></p>
  
  <p>If I right click on those disconnected network drives, I can disconnect them. There must be a way to make those disconnected ones back to available. However, it would not be released by the use net /delete command:</p>
  
  <pre><code>NET USE X: /DELETE
  </code></pre>
  
  <p>The disconnected ones are caused by reboot or logout. It seems that I have no control over them since 1) I use dynamic mapping, and 2)no place to plugin my script before reboot or logout. One way I can think about is to do reconnect by using the following command (simplified one):</p>
  
  <pre><code>IF NOT EXIST Y:\ NET USE \\remotePC\Shared...
  </code></pre>
  
  <p>where Y: may be from a loop. Above is a simplified one. I tried it from cmd and it works. </p>
  
  <p>However before I put it with loop to my batch script, I prefer to clean up all the disconnected ones. That comes to another way: to add some scripts to my batch in start up to do the clean up. If there is any script codes or tools available so that I can use in my batch script to make disconnected ones back to available? </p>