Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I write a command line in a .bat or .cmd that maps a network drive? I want the script to first check if the drive-letter is mapped, and if it is delete it and then map the drive.

I only have the mapping-command right now. Please help me fill in the blanks:

REM Check if drive exists, if it does, delete it

@echo off

net use q: /persistent:yes \\localhost\C$\MyFolder

pause

Are there any of the parameters wrong? Any that should be added?

share|improve this question

4 Answers 4

up vote 10 down vote accepted

You can test for the existence of a drive or folder by testing if the special file "nul" exists in it, i.e.

REM Test if drive exists

IF EXISTS Q:\NUL GOTO Unmap

GOTO Continue

:Unmap

NET USE Q: /DELETE

:Continue

NET USE Q: /persistent:yes \\localhost\C$\MyFolder

Of course, since you are going to delete it anyway, you could simply delete it and not bother checking for existence first.

share|improve this answer
    
Great answer thanks... could you just adjust the script to match my example's directory and I'll accept. Thanks! –  Seb Nilsson May 28 '09 at 9:33
    
+1 for the "nul" –  alexandrul May 28 '09 at 9:35

Delete drive X: with error messages suppressed:

net use X: /DELETE 2> nul

Delete drive X: with both success and error messages suppressed:

net use X: /DELETE > nul 2>&1
share|improve this answer

Don't bother checking for it, just use "net use q: /delete", which deletes it if it exists otherwise it just return an error.

If you then run the script silently using a bit of vb, the error message won't be displayed (nor will the dos window).

share|improve this answer

You could use this command to delete the mapping (no check required):

net use q: /d
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.