Link to home
Create AccountLog in
Avatar of stressedout2004
stressedout2004

asked on

Custom File copy Script

I seek the need to copy a license file to the same directory on 200 computers.

Can someone help me make a script to read a list of IP addresses and or host names that copies the file.
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi there, give this script a try.  Run it with
cscript CopyFile.vbs

and you will see output as it goes.

Regards,

Rob.
strLicenseFile = "C:\Licenses\MyLicenseFile.lic"
strComputers = "Computers.txt"
strTargetFolder = "C:\Program Files\MyApp\License\"
If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInput = objFSO.OpenTextFile(strComputers, intForReading, False)
While Not objInput.AtEndOfStream
	strComputer = objInput.ReadLine
	If Trim(strComputer) <> "" Then
		If Ping(strComputer) = True Then
			strPath = "\\" & strComputer & "\" & Replace(strTargetFolder, ":", "$")
			On Error Resume Next
			objFSO.CopyFile strLicenseFile, strPath, True
			If Err.Number <> 0 Then
				WScript.Echo "Error copying file to " & strComputer & ". Error " & Err.Number & ": " & Err.Description
				Err.Clear
			Else
				WScript.Echo "Copied file to " & strComputer & "."
			End If
			On Error GoTo 0			
		Else
			WScript.Echo strComputer & " is offline."
		End If
	End If
Wend
objInput.Close
WScript.Echo "Done"

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
	Else
		Ping = False
	End If
End Function

Open in new window

Avatar of stressedout2004
stressedout2004

ASKER

That worked like a champ.  Do you know how I can enter either an IP and or Host anme in the computers.txt.  IP addresses do not work.
That seems odd.  I can go to \\192.168.XXX.XXX\C$\Temp and paste a file there.  This should do the same thing...

I tested it with an IP address in the text file as
192.168.XXX.XXX

and it worked fine.  What error do you get?

Regards,

Rob.
IM on Windows 7 and it says error copying to 192.168.1.2:Error 76 Path not found.  But if i use the host name, it works fine. Weird.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account