12

In a batch-file I use the following syntax to map a network drive:

NET USE N: \\someserver\somedirectory.

While this works fine, it also gives an error if the letter n is already used. Is it possible to suppress this error message?

Also, is there a way to unmap a network drive?

I imagine something like:

NET UNUSE N:

3 Answers 3

18

After

NET USE N: \\somserver\somedirectory

to remove simply:

NET USE N: /DELETE

To test for an existing mapping you can;

IF NOT EXIST N:\. GOTO BLABLA
3
  • thanks, this is giving me a question: There are open files and/or incomplete directory searches pending on the connection to N:. Is it OK to continue disconnecting and force them closed? (Y/N) [N]: can this be forced to yes?
    – clamp
    Aug 16, 2011 at 11:23
  • If it is ok do disconnect try ECHO Y|NET USE N: /DELETE
    – Alex K.
    Aug 16, 2011 at 11:32
  • thanks, just to be precise the command is EXIST and not EXISTS
    – clamp
    Aug 16, 2011 at 11:46
2

You can detect if a drive is in use by running

net use N: 

you'll get different return results $? in PowerShell and %ERRORLEVEL% in Batch.

If mapped $? will be True and %ERRORLEVEL% will be 0

To unmap, just do

net use N: /DELETE
0

The easiest is: IF NOT EXIST N:. NET USE N: \someserver\somedirectory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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