Link to home
Start Free TrialLog in
Avatar of awehme
awehmeFlag for United States of America

asked on

Mapping Network Drive by code

Hello everybody,
We are wanting to create a .cmd or .bat file that will map to a certian drive on our network, copy some files over, then disconnect from that network drive.

Can this be done.  We know how to do the copying part, just need help with the Mapping parts.

We are using Windows NT 4.0 and Windows XP Pro

Thanks
ASKER CERTIFIED SOLUTION
Avatar of LRHGuy
LRHGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you don't have a drive letter that will be available on every box you run it on, you'll have to use *, then do another net use to redirect the list of mappings to a text file you then parse in a for loop to find the assigned letter.

I've got the code for that on another box somewhere - lets see if I can find it...
Found it.
This will leave the mapped to drive letter in DriveLetter


set /a LoopCnt= 0
set /a TryCnt= 10
:Mappit

                   *** the following is all one line ***
for /f "tokens=1-8 delims=. " %%a in ('net use * \\SERVERNAME\SHARENAME %Passwd% /user:USERNAME ^| find /I "connected"') do set DriveLetter=%%b
                   *** the above is all one line ***

if exist %lib1%\ goto GotIt
set /a LoopCnt= 1 + %LoopCnt%
if %LoopCnt% GTE %TryCnt% goto BadMapping
goto Mapppit

Avatar of zowyy
zowyy

On my Windows 2000 network at home, the following worked well without requiring a drive mapping:

STEP 1: Establish a resource connection on the remote computer with

  NET USE \\<server>\IPC$

NOTE: this will attempt to log onto the remote computer with whatever user name and password you used to log onto the computer where the .bat file is running.  If you need to register with the remote computer under a different name, use

    NET USE \\<server>\IPC$ /USER:<UserName>

If you need to supply a password, you will be prompted for one by the DOS command line interface.

NOTE: you may need to include a domain as in "google\awehme" (without the quotes, of course) instead of just a user name.

STEP 2: You can then use a command with a direct path using a "\\<server>\<share>" syntax where <server> can be either an IP (such as 192.168.1.2) or the server's text-based name.

I did this successfully on my network using the following commands:

DIR \\192.168.1.3\C$

COPY \\192.168.1.3\C$\CRASH.TXT C:\TEMP\CRASH.TXT

This approach saves the trouble of mapping and unmapping since the IPC$ resource can remain connected indefinitely (it will drop out when you reboot).  You can also use it to connect to multiple network shares and move files between them without tying up drive letters--preventing possible conflicts if the drive letter is already in use.

In fact, if your user account and password are the same on both the computer running the .bat file and the network server(s), you might try it without the NET USE \\<server>\IPC$ command.  I just used IPC$ because my user names were different on the two computers.
Avatar of awehme

ASKER

Thanks guys.....I'm a newbie as you tell.

Answer from LRHGuy is all I neeeded, but cookre's answer will come in handy down the road.

Thanks