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?

link|improve this question
feedback

4 Answers

up vote 8 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.

link|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
feedback

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

net use q: /d
link|improve this answer
feedback

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).

link|improve this answer
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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