6

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?

4 Answers 4

11

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.

1
  • Great answer thanks... could you just adjust the script to match my example's directory and I'll accept. Thanks! May 28, 2009 at 9:33
2

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

net use q: /d
2

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

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .