Solved

Mapping Network Drive by code

Posted on 2004-08-25
6
281 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 125
Counting documents in a Domino View 3 66
C# application error "Parameter Is Not Valid". 3 79
Copy dir and files with robocopy 2 35
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now