This FileCopy.vbs script performs a very simple task - it copies a file to the same directory on a number of computers.  A list of computers is provided in a test file named by default INPUT.TXT and then performs the file copy to each computer listed.  The script also creates an output log file named by default <input file name>_LOG.TXT.  The script can be easily modified to copy multiple files to multiple destinations if needed.

The real beauty of this script is it can be used a template for many useful scripts that require you to perform some task against a large number of computers.  Just replace the code within the "DO WHILE" loop with you own code and you are off and running.  The rest of the script is boilerplate code to read an input file and write to a log file.  I have used this template to quickly create scripts dozens of times.

Code Review
The script code is well commented to make it easy to understand and modify.  Here is a brief discussion of what happens:

You are prompted for to enter the name of the input file.  By default INPUT.TXT is selected and is expected to be in the same folder.  You can use any file in any folder as long as you type in the full path and name.  Then you are prompted to give the output file a name.  By default the input file name will be appended with _LOG.TXT but again you can type any name and path you choose.

Next the input file is opened.  If the file cannot be found and errors is displayed and the script exits.

The next block of code is commented out.  Uncomment if you want a script header.  I tend to not use headers/footers and create plain CSV logs so I can open them in Excel for easy analysis.

Now the DO WHILE loop begins.  The first line of our input file and store the name in the "strComputer" variable.  Then a function named GetComputerStatus is called to confirm the computer we are about to work on is on line and can be reached.  GetComputerStatus is nothing fancy, just a ping command and looking for a "reply from".  The function returns a "1" if the computer responds or a "0" if no response.  The commands to perform a the file copy are used if a "1" is detected and a file named "test1.txt" is copied to the "C:\Documents and Settings" folder on each computer in the input file.  A variable called "strComputerStatus" will store the error code returned by the copy operation (0 if no error) and the error description for use in the log file.

Next a line is written to the log file.  Each line will contain the computer name (strComputer), a comma, and the error (strComputerStatus) code if any.

Next the error buffer is cleared and the script loops back to the top and the same steps are performed on the next computer name until we reached the end of the input file.

Next the log is closed (saved). 

The log file is opened in Notepad for review.

Using The Code 
To use the script code, copy all the text between the <BEGIN SCRIPT> and <END SCRIPT> tags into a text file and give it a VBS extension when you name it.  Create an input file with one computer or IP address on each line.  The script can be run in Windows by double clicking it.  A DOS window will open each time a computer is pinged.  You might want to run this in a DOS box using CSCRIPT.EXE FileCopy.vbs to prevent the DOS pop ups.



'  SCRIPT: FileCopy.VBS
'  AUTHOR: DonBaker
'    DATE: 01/02/2007
' VERSION: 1.0
' PURPOSE: Used to copy the same file to a number of computers
'   USAGE:
'Revision: 00/00/00 - change

Option Explicit


Dim oWshShell, oFSO, oFileName1, oFilename2, objWMIService, colItems, sProtocol, sSearch, sNWStatus, sDate, iErrNumber
Dim objItem, strComputer, oExec, strPingStdOut, sStatus, bComputerOnline, aComputers, Computer, sOutPutFile, sInPutFile, sComputerStatus
Dim arrFileNAme, sOutPutFileName
CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
Const OverwriteExisting = TRUE

'Prompt for name of input file
sInPutFile = INPUTBOX("Enter name of input file.  Input file must exist in the script folder.", "Enter Input File Name","input.txt" )
IF sInputFile = "" THEN
   wscript.echo "Operation was cancelled"

'Autoname output file
'Trim extension from sInputFile1  
arrFileNAme = Split(sInPutFile, ".")
sOutPutFileName = UCASE(arrFIleNAme(0))
'Prompt for name of output file
sOutPutFile = INPUTBOX("Enter name of output file.  Output file will be placed in script folder.", "Enter Output File Name",sOutPutFileName & "_LOG.TXT" )
IF sOutPutFile = "" THEN
   wscript.echo "Operation was cancelled"


Set oWshShell = Wscript.CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

'Open input file and read
Set oFilename1 = oFSO.OpenTextFile(".\" & sInPutFile, ForReading, False)
iErrNumber = err.number
  'Check for missing file
  IF iErrNumber = 53 THEN
     Wscript.echo "Error - " & sInPutFile & " file was not found."

Set oFilename2 = oFSO.OpenTextFile(".\" & sOutPutFile, ForWriting, True)

'Get date and write it to log

'Uncomment next three lines if you want log start header
 'sDate = Now()
 'oFilename2.writeline "Log Started " & sDate
 'oFilename2.writeblanklines 1

'Read external list of computers and perform some work
DO While oFilename1.AtEndOfStream <> True
    'read computernames from the input file
    strComputer = oFileName1.ReadLine
    'confirm computer is online
    IF GetComputerStatus(strComputer) = 1 Then
    'If computer is online do some work
        '<<<.modify line below to set the source and destination locations>>>
        oFSO.CopyFile ".\test1.txt" , "\\" & strComputer & "\C$\Documents and Settings\" , OverwriteExisting
        sComputerStatus = err.description & " " & err.number
      'if not online  do something else
        sComputerStatus = "Offline"
    End IF

    'Write to log file
    oFilename2.writeline strCOmputer & "," & sComputerStatus
    'reset error buffer

sDate = Now()
'Uncomment next two lines if you want log stop footer header
 'oFilename2.writeblanklines 2
 'oFilename2.writeline "Log Completed " & sDate

'Close input file
'Close Log file

'Wscript.echo "Finished Scanning Computers" "notepad.exe .\" & sOutPutFile, 5, FALSE

Set oWshShell = Nothing
Set oFSO = Nothing
Set oExec = Nothing
Set oFilename1 = Nothing
Set oFilename2 = Nothing

Function GetComputerStatus (strComputer)
  'Function Returns a 1 if computer is available
  'Used to determine if a computer is online before
  'attempting WMI connection
  'IP Address or computer name can be used
  Dim sStatus
  sStatus = 0
  Set oWshShell = Wscript.CreateObject("Wscript.Shell")
  Set oExec = oWshShell.Exec("ping -n 2 -w 1000 " & strComputer)
  strPingStdOut = LCase(oExec.StdOut.ReadAll)
    If InStr(strPingStdOut, "reply from") <> 0 Then
      sStatus = 1
      sStatus = 0
    End IF
    GetComputerStatus = sStatus

***<END SCRIPT>***


Comments (3)

  1. Frank says:

    Tried your script on xp home w/service pack 2 I get an error. Its a message box that is titled Windows script host and it reads Line: 1 Char:1 Error: unexpected statement code: 800A0400  Source Microsoft VBScript compliant error plaese email me any comments — Thanks, Frank

  2. MuadDib says:

    re: FileCopy Error

    SInce the error occured at line one char 1 you have probably copied the line ***<BEGIN SCRIPT> *** into your script.  Delete this line and make sure the first line is the commented line shown below. The apostrophe  character (‘) is how VBS scripts are commented.


Skip to main content