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


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


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


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

| 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

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

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

| improve this answer | |

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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