4

I want to write a start up script to take an mapped drive, change the drive letter, then put a different share on the original drive. How can this be done?

Bob
  • 2,937
  • 5
  • 29
  • 32
  • Check out the answer to this question as it might help you in capturing the drive path prior to the delete command: http://stackoverflow.com/questions/840677/how-can-i-get-a-drive-list-from-a-batch-file – Kevin Kuphal Aug 03 '09 at 21:14

3 Answers3

3

Absolutely.

If for example the existing drive is X: and has \server1\shareA on it and you wanted to remap X: to Y:you could do it with a batch script.

net use x: /delete
net use y: \\server1\shareA

If you need to pass credentials you'd have to add the username (and possibly the password if you want it to run totally automated. Note that it's a bad idea to do this with privileged accounts and there are way smarter ways. But for a quick change over this will do it

net use x: /delete
net use y: \\server1\shareA <password> /user:<username>

If you don't include the password it will prompt. You can save this in a .bat file and it will run just fine.

EDITED TO ADD more complete solution

So you want to take a drive mapping X: change it to Y: and then connect X: to the new share \server1\newshare? Here you go. You can of course still pass credentials if necessary.

for /F "skip=1 tokens=3" %%i IN ('net use x:') = DO (
        set OLDSHARE=%%i
        goto :DONE
        )
:DONE

net use x: /delete
net use y: %oldshare%
net use x: \\server1\newshare

The for loop parses out the existing share path for the drive letter you want to change. Then you disconnect it from x: reconnect it to y: and then connect the new thing to x: all in quick succession.

Laura Thomas
  • 2,845
  • 1
  • 28
  • 24
  • Is it possible to pick up the location of the existing share before deleting it? – Bob Aug 03 '09 at 21:01
  • You mean determine it from the script? Possibly with a bit of fancy parsing. net use with no arguments returns all the mapped network shares. Net use with only the drive letter returns information including the remote name. – Laura Thomas Aug 03 '09 at 21:25
  • I edited my answer above. Hopefully this is what you want. It's sort of quick and dirty but it works. – Laura Thomas Aug 03 '09 at 22:25
  • Hmm, I'm trying to figure out what question you answered with "Absolutely". ;) – Sasha Chedygov Aug 04 '09 at 04:58
1

Check out the NET USE command.

You will need to delete the current mapping, then remap with the desired drive letters and shares.

DCNYAM
  • 1,029
  • 7
  • 14
1
net use X: /DELETE
net use X: \\newshare

Where X: is the drive letter you want to map and \\newshare is the location of the new share you want mapped

Ciaran
  • 186
  • 1
  • 1
  • 6