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

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?

share|improve this question
    
Check out the answer to this question as it might help you in capturing the drive path prior to the delete command: stackoverflow.com/questions/840677/… –  Kevin Kuphal Aug 3 '09 at 21:14
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
Is it possible to pick up the location of the existing share before deleting it? –  Bob Aug 3 '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 3 '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 3 '09 at 22:25
    
Awesome, thanks a lot! –  Bob Aug 3 '09 at 23:20
    
Hmm, I'm trying to figure out what question you answered with "Absolutely". ;) –  musicfreak Aug 4 '09 at 4:58
add comment

Check out the NET USE command.

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

share|improve this answer
add comment
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

share|improve this answer
add comment

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.