Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows bat file: undo NET USE

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:

like image 563
clamp Avatar asked Aug 16 '11 11:08

clamp


People also ask

How do I undo net use?

One method that works in all modern versions of Windows is to use the Command Prompt. Open it and type: net use drive letter /delete. Then, press Enter. For example, we have a drive mapping using the letter Z, so we have to type: net use Z: /delete.

What is net use in batch file?

Connects or disconnects your computer from a shared resource or displays information about your connections.


2 Answers

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
like image 127
Alex K. Avatar answered Oct 19 '22 02:10

Alex K.


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
like image 42
Scott Weinstein Avatar answered Oct 19 '22 03:10

Scott Weinstein