Advanced File Copy Script

By | October 27, 2006

Here is a script which will not only copy files specified in an XML file but also log weather the file copy worked or not and insert a registry key to check if filecopy has run before so it doesn’t run it again. I use this for copying altered remote assistance files to my remote computers via Group Policy.

For this script to work you will need 3 files

filecopy.js
filecopy.xml
filecopylog.csv

For the first file, filecopy.js

———-COPY EVERYTHING BELOW THIS LINE———-
var WshShell = WScript.CreateObject(“WScript.Shell”);
var env = WshShell.Environment(“PROCESS”);
var WshNetwork = WScript.CreateObject(“WScript.Network”)

var clientversion, fs, f1, f2, clientname, scriptpath, ForReading, ForAppending, fileCopyXML, serverversion, objXML;
var xmlcatagories, instructions, xmlNode, currentXMLNode, src, dst, status;
fileCopyXML = “filecopy.xml”;
clientversion = 0;
status = 1
ForReading = 1;
ForAppending = 8;

objXML = new ActiveXObject(“Microsoft.XMLDOM”);
fs = new ActiveXObject(“Scripting.FileSystemObject”);

try
{
scriptpath = fs.GetParentFolderName(WScript.ScriptFullName);

objXML.load(scriptpath+”\\”+fileCopyXML);

if (objXML.parseError.errorCode != 0)
{
var myErr = objXML.parseError;
WScript.Echo(“You have error ” + myErr.reason);
}
else
{
serverversion = objXML.selectSingleNode(“//configuration/version”).text;
clientname = objXML.selectSingleNode(“//configuration/clientname”).text;

try
{
clientversion = WshShell.RegRead(“HKLM\\Software\\FILECOPY\\”+clientname+”\\FileCopy”);
}
catch(e)
{
WshShell.RegWrite (“HKLM\\Software\\FILECOPY\\”+clientname+”\\filecopy”, 0, “REG_DWORD”);
}

if (clientversion < serverversion)
{
xmlNode = objXML.selectSingleNode(“//operations/copy”);
for (i = 0; i < xmlNode.childNodes.length;i++)
{
try
{
currentXMLNode = xmlNode.childNodes.item(i);
src = currentXMLNode.getAttribute(“src”);
dst = currentXMLNode.getAttribute(“dst”);

if (dst.indexOf(“%”) >= 0)
dst = dst.split(“%”)[0]+””+env(dst.split(“%”)[1])+””+dst.split(“%”)[2];

f1 = fs.GetFile(scriptpath+”\\srcfiles\\”+src);
f1.Copy(dst);

f2 = fs.OpenTextFile(scriptpath+”\\filecopylog.csv”, ForAppending, false);
f2.WriteLine(WshNetwork.ComputerName+”, successfully copied, “+dst);
f2.Close();
}
catch(e)
{
status = 0;
try
{
f2 = fs.OpenTextFile(scriptpath+”\\filecopylog.csv”, ForAppending, false);
f2.WriteLine(WshNetwork.ComputerName+”, failed to copy, “+dst);
f2.Close();
}
catch(s)
{
}
}
}
xmlNode = objXML.selectSingleNode(“//operations/delete”);
for (i = 0; i < xmlNode.childNodes.length;i++)
{
try
{
currentXMLNode = xmlNode.childNodes.item(i);
src = currentXMLNode.getAttribute(“src”);

if (src.indexOf(“%”) >= 0)
src = src.split(“%”)[0]+””+env(src.split(“%”)[1])+””+src.split(“%”)[2];

if (fs.FileExists(src))
fs.DeleteFile(src);

f2 = fs.OpenTextFile(scriptpath+”\\filecopylog.csv”, ForAppending, false);
f2.WriteLine(WshNetwork.ComputerName+”, successfully deleted, “+src);
f2.Close();
}
catch(e)
{       
status = 0;
try
{
f2 = fs.OpenTextFile(scriptpath+”\\filecopylog.csv”, ForAppending, false);
f2.WriteLine(WshNetwork.ComputerName+”, failed to delete, “+src);
f2.Close();
}
catch(s)
{
}
}
}
}
if (status)
WshShell.RegWrite (“HKLM\\Software\\FILECOPY\\”+clientname+”\\filecopy”, serverversion, “REG_DWORD”);
}
}
catch(x)
{
}
———-COPY EVERYTHING ABOVE THIS LINE———-

 

The next file is filecopy.xml. This file holds the variables and filename paths for the script above. The XML file should look like this

 

———-COPY EVERYTHING BELOW THIS LINE————
<?xml version=”1.0″ ?>
<filecopyxml>
<configuration>
<version>1</version>
<clientname>COMPANY_NAME</clientname>
</configuration>
<operations>
<copy>
<file xsrc=”helpeeaccept.htm” dst=”%SystemRoot%\pchealth\helpctr\system\Remote Assistance\helpeeaccept.htm” />
<file xsrc=”TakeControlMsgs.htm” dst=”%SystemRoot%\pchealth\helpctr\system\Remote Assistance\Interaction\Server\TakeControlMsgs.htm” />
</copy>
<delete>
</delete>
</operations>
</filecopyxml>
———-COPY EVERYTHING ABOVE THIS LINE———-

And the last file is filecopylog.csv. All you need to do here is create filecopylog.csv in the same folder.

 

The next part is to make sure everything is in the correct spot. I will explain how I have it for simplicity sake.

All files are located in a server share

\\SERVERNAME\SCRIPTS

In this folder are 4 things(3 files and 1 folder):
filecopy.js
filecopy.xml
filecopylog.csv

srcfiles

Inside the srcfiles folder are the file specified in the XML document(TakeControlMsgs.htm and helpeeaccept.htm)

 

Credit for this script goes to James Currie

This script is provided “AS IS” with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *