Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a user who needs to map a certain network location to drive O:. Every so often, this drive loses it's map to O:, but will show up under different letters, such as H:, K:, and L:. The normal procedure is for him to call Helpdesk (me) to come down and unmap/remap the drive to the proper letter.

During the remap process, I make sure to check the box Remap on login but that setting doesn't seem to take hold.

Is there any way to script this task? Normally, I would use net use /delete \\server\share or net use x: /delete (where x: is the drive letter), but the first one does not work as there is a letter assigned to the path, and the second one doesn't work as the letter it remaps to isn't always the same one.

System is Windows NT (we use specialized software that hasn't had an update in a while).

share|improve this question
1  
+1 for Windows NT. –  Tanner May 14 '13 at 15:14
1  
Write a script (“batch file”) to do net use and pipe it through find or findstr to identify the drive letter that the share is currently mapped to. –  Scott May 14 '13 at 17:31
    
@Scott Can you expand on that script? I have very little scripting knowledge within Windows. –  Kruug May 14 '13 at 20:35

2 Answers 2

up vote 2 down vote accepted

As long as there are no other shares you are trying to save you can use the following in a batch or logon script:

 NET USE * /delete
 NET USE O: \\server\share

This will remove all assigned (*) shares and set it to the drive/path you want.

share|improve this answer
    
Yeah, there are other shares. Do you have one that works with the find or findstr that Scott mentioned above? –  Kruug May 15 '13 at 4:16
    
It can be done but not easily with DOS batch scripting. Finding the share is easy but acting upon it is much more difficult and means a more complex and bloated script. Is only one user affected? Can you add the additional shares after the code listed above? –  edwardbackstrom May 15 '13 at 4:36
1  
Actually, there is no real reason to "over-tech" the problem. As long as the share name is correct, you should be able to just use NET USE \\server\share /delete instead of NET USE * /delete. Then continue and assign it to the correct drive letter. –  edwardbackstrom May 15 '13 at 4:48
    
Yeah, I tend to have that issue (over-teching) when looking at a problem. –  Kruug May 15 '13 at 13:14

To identify the drive letter that the share is currently mapped to and unmap it:

@echo off
for /f "tokens=2" %%D in ('net use ^| find ":" ^| find "\\server\share") do net use %%D /delete

Comments:

  • ^ is like \ in Unix –– it turns the following character from special to non-special, or vice-versa.  We use ^| so that the interpretation of the | characters is deferred until the command inside the parentheses is parsed.
  • %%D is a FOR loop index variable.  You can use any letter you want, but, unlike normal Windows Command Prompt variables,
    • it can be only a single letter, so don’t try to use %%DRV, and
    • it is case-sensitive.
  • I included the find ":" because, when I was testing this, I got a  net use  output that looked like
OK           H:           \\WONDERLAND\HUMPTYDUMPTY
OK           Q:           \\WONDERLAND\QUEENOFHEARTS
OK                        \\WONDERLAND\CHESHIRECAT

So you need to do something to filter out the lines that don’t include a drive letter.

  • As you may know (because I hinted at it, above), FOR is a loop construct.  Your statement(s) get executed once for each line that comes out of the net use ... | find pipeline, with %%D set to the second string (token) on the line.  If you want to do more than one command, and/or be more selective, you can do this:
for /f "tokens=2" %%D in ('net use ^| find ":" ^| find "\\server\share") do (
    ...
    if %%D==Z: (
        ...
    ) else (
        ...
    )
    ...
)

Want more?  Change "tokens=2" to "tokens=2,3", and %%E will be set to the third token; i.e., the UNC that the drive letter is mapped to.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.