Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Mapping Network Drive by code

Posted on 2004-08-25
6
283 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:awehme
6 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 300 total points
ID: 11892976
Did you try:

Net Use T: \\servername\sharename

to connect and

Net Use T: /DELETE

to disconnect?

"T:" is the drive you use for the copy.
0
 
LVL 22

Assisted Solution

by:cookre
cookre earned 200 total points
ID: 11892984
You can easily do that with 'net use' :

NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]
0
 
LVL 22

Expert Comment

by:cookre
ID: 11893090
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...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 22

Expert Comment

by:cookre
ID: 11895498
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

0
 

Expert Comment

by:zowyy
ID: 11899696
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.
0
 
LVL 2

Author Comment

by:awehme
ID: 11909693
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question