Script that copies a single file to numerous computers

Hi Techies
I recently had to manually copy a file from a server share to 30 pc;s, I'm sure there must be an automated script I could run. The details are:-

Copy a single file, which will be the same every time from a UNC server path to different desktop machines, the path will always be the same for the desktops.

Is there a batch file or some kind of script I could run which would prompt for the path of the server, then enter filename.

From here if you could then say take input from a text file with the name of all the machines needs copying to, and the path to these machines which is always the same.

There may already be a script out there that does this or I can modify to suit?

If there is any flexibility in selecting filename or paths, maybe a browse dialog box, then even better

Please assist

MartinP1969Technical Support AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try a batch file with something like this...
(stolen from code to search directories from a serverlist.txt file)
serverlist.txt contains
The server list has the folders you are copy to, and the original file is hard coded.  easy to change this if you need more functionality.  save it as copyfiles.bat (or something similar)
@echo off
set servlist=c:\serverlist.txt
if not exist %servlist% echo %servlist% not found & pause & goto end
for /f "tokens=1,2*" %%a in (%servlist%) do call :process %%a %%b
goto end
COPY C:\testfile.txt \\%1\%2

Open in new window

try this sub routine...

sub copystuff(byval ComputerName)
      strhere = "c:\download_file_abc.txt"      'copy from
      strthere = "\\" & ComputerName & "\c$\Folders\download_file_abc.txt"    'copy to
      Set fso = CreateObject("Scripting.FileSystemObject")
      fso.CopyFile strhere, strthere,TRUE
end sub
You can also try this VBS as well, which gives you a dialog to find the source file.


strInputFile = "Computers.txt"
' Specify the path as if it were local to the machine
' It will be converted to the admin share of the drive
' For example, if you use C:\Temp
' it will be converted to \\machinename\c$\temp
strDestinationLocal = "C:\Temp"
If Right(strDestinationLocal, 1) <> "\" Then strDestinationLocal = strDestinationLocal & "\"
strLogFile = "FileCopyLog.log"
' Get the file path to be copied
Set objDialog = CreateObject("SAFRCFileDlg.FileOpen")
intResult = objDialog.OpenFileOpenDlg
If intResult Then
	strSourceFile = objDialog.FileName
	strFileName = Mid(strSourceFile, InStrRev(strSourceFile, "\") + 1)
	strResults = "Copying file to computers:"
	' Go through the machines
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
	While Not objInputFile.AtEndOfStream
		strComputer = objInputFile.ReadLine
		If Ping(strComputer) = True Then
			strTarget = "\\" & strComputer & "\" & Replace(strDestinationLocal, ":", "$") & strFileName
			On Error Resume Next
			objFSO.CopyFile strSourceFile, strTarget, True
			If Err.Number <> 0 Then
				On Error GoTo 0
				strResults = strResults & VbCrLf & "Failed to copy " & strSourceFile & " to " & strTarget
				On Error GoTo 0
				strResults = strResults & VbCrLf & "Successfully copied " & strSourceFile & " to " & strTarget
			End If
			strResults = strResults & VbCrLf & "Could not ping " & strComputer
		End If
	Set objInputFile = Nothing
	Set objOutputFile = objFSO.CreateTextFile(strLogFile, True)
	objOutputFile.Write strResults
	Set objOutputFile = Nothing
	MsgBox "Finished. Please see " & strLogFile
	MsgBox "No file was selected. Script cancelled."
End If
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 4 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
		Ping = False
	End If
End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

The script will prompt the following answer
Pc list[leave empty for AUTOMATIC/file]? =   if empty the list will be automatically generated
File to be copied? =     absolute path to the file UNC or local
Destination directory? =  is the share on every pc
and  then start to copying...

Bye Gas
:: capy-all.cmd
:: copy a file in all pc
:: gastone canali  
@ECHO off
SETLOCAL EnableDelayedExpansion
set /p pclist=Pc list[leave empty for AUTOMATIC/file]? =
set computers=type "%pclist%"
if not exist "%pclist%"  echo "Computer list automatically generated"   
if not exist "%pclist%"  set computers=net view^^^|find /i "\\"
echo  File Ex.: \\server1\accounting\read me.txt
set /p file=File to be copied? =
if not exist "%file%" goto :_ERR
echo Source file: %file%
echo  Destination directory Ex.: temp
set /p dest=Destination directory?
echo The file %file% will be copied on all \\pc\%dest%
for  /f "delims=\" %%c in ('%computers%') do call :_DO %%c 
goto :_END
set cmd=copy /y "%file%" "\\%1\%dest%"
set alive=yes
ping -n 1 -4 -w 300 %1 |find /i "ttl" >nul || set alive=no
if +!alive!+==+no+  ( 
   echo %1 --- switched 0ff 
) else (
   %cmd% 2>nul|find /i "0 file" >nul  && echo Failed to copy in "\\%1\%dest%" ||echo Successfully copied in "\\%1\%dest%" 
goto :_EOF
 echo error! File not found...
goto :_EOF

Open in new window

MartinP1969Technical Support AnalystAuthor Commented:
Sorry for the delay, some personal stuff kicking off.

I would like to award the points to Rob Sampson, whose answer fit the bill the most and was slick and informative

I have upped the points from 200 to 300, as I would like to extend it a little bit and the points are yours Rob

1 Appears only can select from C:, not any folders or subfolders?

2 Can you include it for network drive selection vias the GUI

3 Possible to select the destination of the file via GUI to include network frives and folders? or at least locally, not relying on hard coded destination

I would like to thank you all for your efforts, you make IT happen
Will respond quickly if get the little amenmemts

Thanks Again

MartinP1969Technical Support AnalystAuthor Commented:
Thanks Rob, quality answer, if you have the time, please see my last comments, you may be able to tweak it a bit. I award all points, plus bonus points  up front as gesture.

Thanks to all who contributed


Hey Mart, sorry for my late reply.  Thanks for the grade by the way.

For your questions 1 and 2, on my system, I can select any network drive from the top of the dialog, and any I'm not sure why you are unable to.

For question 3, unfortunately, no. VBScript does have a "folder picker" implementation of the dialog.  It can only select individual files.  You could, however, have the destination entered as a text string by the user by changing this line:
strDestinationLocal = "C:\Temp"

to this:
strDestinationLocal = InputBox("Enter the destination path:", "Destination Folder", "C:\Temp")


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.