Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File copy with a difference

Status
Not open for further replies.

tomvear

IS-IT--Management
Oct 25, 2006
37
GB
Hi i feel stupid for asking but i cant find a script to put into a logon script that will copy a set of files to every client pc unless they already have them, but i need it so that it checks the date and time on the source file and only copies it to the client computer if it is newer or doesnt exist on the client pc at all, this was the closest thing i got but it doesn work!!! lol help please!

<%
dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")
set demofile = filesys.CreateTextFile ("c:\somefile.txt", false)
set demofile = filesys.GetFile("c:\somefile.txt")
demofile.Copy("c:\projects\someotherfile.txt")
%>
 
Read the documentation for the FileSystemObject and it's related File object. You will want to compare the create date for each file to determine if it should be copied or not.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I'm working on doing this for our network as well. My best example is with all files in a directory because it seems to work very well when you are working with File and Directory objects:
Code:
Dim fs, objFolder, objFiles, objFile
Dim strMasterFilePath, objMasterFile

Set fs = CreateObject("Scripting.FileSystemObject")
Set objFolder = fs.GetFolder("c:\test")
Set colFiles = objFolder.Files

For Each objFile in colFiles
	strMasterFilePath = "c:\master\" & objFile.Name
	Set objMasterFile = fs.GetFile(strMasterFilePath)
	If objMasterFile.DateLastModified>objFile.DateLastModified Then
		objFile.Copy(strMasterFilePath)
	End If
	Set objMasterFile = Nothing
Next

Set colFiles = Nothing
Set objFolder = Nothing
Set fs = Nothing
My script is also a work in progress but it sounds like that is what you're trying to accomplish with your script. Once you have a file object you can call the DateLastModified property for comparing the two files.

Hope this helps!
 
im fairly new to vbs, the code i posted is about all i know about vbs!
 

do i just run this as normal????


Dim fs, objFolder, objFiles, objFile
Dim strMasterFilePath, objMasterFile

Set fs = CreateObject("Scripting.FileSystemObject")
Set objFolder = fs.GetFolder("c:\test")
Set colFiles = objFolder.Files

For Each objFile in colFiles
strMasterFilePath = "c:\master\" & objFile.Name
Set objMasterFile = fs.GetFile(strMasterFilePath)
If objMasterFile.DateLastModified>objFile.DateLastModified Then
objFile.Copy(strMasterFilePath)
End If
Set objMasterFile = Nothing
Next

Set colFiles = Nothing
Set objFolder = Nothing
Set fs = Nothing
 
You could just run that code as normal, assuming that "c:\test" matched a directory where you wanted to iterate through all files and compare them with files of the same name in "c:\master\"

In posting this response I realize that my original set was a bit backward for how I prefer to do things. I like to have a "master" directory, which would be the original GetFolder. I would then iterate through all files in it and compare them with the files in a different location, copying in files that didn't exist and replacing ones that did not match the DateLastModified.

The long and short of it is that the code I gave is an example, which should fully work if the paths in GetFolder("c:\test") and strMasterFilePath = "c:\master\" are changed to match the ones on your system.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top