Question Locked

Looping Net Use Batch File

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello everyone,

I would like to find out if it is possible to loop through a series of networked computers, access their drive through netuse, xcopy a file, and then release the share.

I wrote a working batch file that will do it one PC at a time, but being able to loop would be a huge help.

Here's what I envision, please let me know how to do it properly, if it's possible:

For each i = 1 to 20
net use x: \\PC i\c$
xcopy x:\users\file\* z:\Temp\PC i /y
net use x: /delete
Next

Is it possible to append the variable "i" (PC i) onto the sharename while using netuse and xcopy?

Thank you!

4 replies

by rob miners

and Duplicate file names will be a problem as they will be overwritten if they exist.

You will require two Batch files. One is for the connection and the other will hold the computer names and call the file for the connection.

These are the contents of the call Batch file collect.cmd. The computer name will be the variable for %1%.
==================
call c:\xdcopy.cmd computer1
call c:\xdcopy.cmd computer2
call c:\xdcopy.cmd computer3
call c:\xdcopy.cmd computer4 and so on

==================

These are the contents of the connection Batch file xdcopy.cmd.
==================
net use X: \\%1%\c$ /user:administrator password
xcopy x:\users\file\*.* z:\Temp\PC /y
net use x: /delete

goto end

:END

================

It's very basic but it should work. Someone may have a better way of doing it and I'm sure that they will let us know.

by batz281

What will actually tell the xdcopy.cmd file how to initialize the %1% variable?

Does %1% get filled by putting the computer name after "xdcopy.cmd", such as "xdcopy.cmd Computer1"? And if so, can that be used in the xcopy command, such as "z:\Temp\PC\%1% /y"?

The files have unique names but need to be placed in their own directory, named after the PC they came from.

Thanks again :-)

by rob miners

%1% = the computer name. You will have to create the folder name for each System. this is achieved using mkdir. echo y| will force the disconnection from the drive X:.

net use X: \\%1%\c$ /user:administrator password
mkdir "Z:\temp\pc\%1%"
xcopy x:\users\file\*.* z:\Temp\pc\%1% /y
echo y| net use x: /delete

goto end

:END

Keep us informed as to your progress if you require further assistance. ;) :D


If you think that any of the posts that have been made by all TechRepublic Members, have solved or contributed to solving the problem, please Mark them as Helpful so that others may benefit from the outcome. ;) :D :-bd


How do I rate the answers to my posted Question?
Click on the answer. Click the Mark "Helpful" button displayed below the post. You may mark more than one answer as "Helpful."

This discussion is closed to new replies.