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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Checking and Comparing???

Status
Not open for further replies.

tEkHEd

IS-IT--Management
Jan 29, 2003
261
GB
Hi..

I have some code that I am working on that I am having a little problem with...

I have 2 x files.. SrcFile and DestFile. I have to check if the SrcFile and the DestFile have the same date stamp. If they do, then the script should exit, if not the source file will be written as the destination file, overwriting any existing file...

The script I have come up with is:

Code:
Const OverwriteExisting = True

Dim objSrcPath
Dim objDestPath
Dim objSrcFile
Dim objDestFile


set objFSO = CreateObject("Scripting.FileSystemObject")

'Sets Source path
set objSrcPath = objFSO.GetFolder("\\Servername\Sharename")
'wscript.echo objSrcPath

'Sets Destination path
set objDestPath = objFSO.GetFolder("\\Servername\Sharename\Foldername")
'wscript.echo objdestpath

'Sets Source file name
set objSrcFile = objFSO.GetFile (objSrcPath & "\Filename.TST")
'wscript.echo objsrcfile

Set objDestFile = objFSO.GetFile (objDestPath & "\Filename.TST")



If objFSO.FileExists(objSrcFile) Then 'If Source file exists
    If DateDiff(&quot;d&quot;, (objSrcFile.DateCreated), (objDestFile.DateCreated)) <> 0 Then 'check the date stamp
        wscript.echo objsrcfile.DateCreated
        wscript.echo objdestfile.DateCreated
        wscript.echo &quot;files Don't have the same date stamp&quot;
        objFSO.CopyFile objSrcFile, objDestFile, OverwriteExisting
        'If source and dest files do not have the same date stamp copy the file.
        Else
        wscript.echo objsrcfile.DateCreated
        wscript.echo objdestfile.DateCreated
        wscript.echo &quot;Files are the same&quot;
    End If
End If

The problem that I am having is that the DateCreated is too accurate (i.e. it reports the time as well as the date) so the files need to be written at exactly the same time for the script to pick up that they are the same. This method would work if I could MIRROR the file rather than copy it, as it would then copy the same created date and time... is there a syntax for that?

I though about using:

Code:
If DateDiff(&quot;d&quot;, (objSrcFile.DateCreated), Date) <> 0

But I am not sure if this would be accurate enough.. alternatively I suppose that I could try a binary/CRC check of some description???

The only other thing that I can think of is to:

1: mirror all files with robocopy, this gives a starting point
2: the script above should confirm that files are the same
3: any new files at the source, would initiate a robocopy script to re-mirror the src to the dest..

This is a bit dirty tho imo.. I would prefer to work out a solution within 1 script rather than 2. The script would also be run as part of a DTS package (SQL2K) so again, if this can be done without copying additional files onto the live sql server, that would be the preferred solution.

Any ideas are appreciated..

Ta :¬)
 
The problem you are experiencing is not in the DateDiff part of your code. You are not echoing what your DateDiff is checking. Change your echos to:

wscript.echo DatePart(&quot;m&quot;,objsrcfile.DateCreated) & &quot;/&quot; & DatePart(&quot;d&quot;,objsrcfile.DateCreated) & &quot;/&quot; & DatePart(&quot;yyyy&quot;,objsrcfile.DateCreated)

wscript.echo DatePart(&quot;m&quot;,objdestfile.DateCreated) & &quot;/&quot; & DatePart(&quot;d&quot;,objdestfile.DateCreated) & &quot;/&quot; & DatePart(&quot;yyyy&quot;,objdestfile.DateCreated)

This will ensure that your are seeing what your DateDiff is seeing.

Otherwise your code should work, except that if the source file is older than the destination file you will overwrite the newer destination file with the older source file.

You could change your code to:

If DateDiff(&quot;d&quot;, (objSrcFile.DateCreated), (objDestFile.DateCreated)) < 0 Then

This would ensure that only newer source files would overwrite older destination files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top