0

I'm trying to synchronize .doc files, based on their dateLastModified Attribute, using WSH and Jscript.

I have a folderObject called src and a string to destination, called dstPath.

the src folderObject points to a remote server. But i tried with both folders on local disk, with same results

var fso = WSH.CreateObject("Scripting.FileSystemObject");
src = fso.getFolder(srcPath);

for(e = new Enumerator(src.files) ; !e.atEnd() ; e.moveNext() ){

//only word docs
if ( e.item().Type == "Document Microsoft Word 97 - 2003"){

    try{
        f= fso.getFile(dstPath + e.item().Name)

        if ((f.DateLastModified) != (e.item().DateLastModified)){
            WSH.echo(f.Name + "\t" +  f.DateLastModified + "\t" +  e.item().DateLastModified)
            e.item().copy(dstPath);
        }

    }
}

Here is some sample output :

test1.doc   Tue Jun 10 11:10:04 UTC+0200 2014   Tue Jun 10 11:10:04 UTC+0200 2014

as you can see, both dates are the same, but the != operator still returns true, and the file get copied anyway...

I also notice that typeof(file.dateLastModified) returns "date" but this is not a real Jscript date object, since I cannot do f.DateLastModified.getTime() it returns nothing.

Any hints on what's wrong ?

1 Answer 1

2

Your

if ((f.DateLastModified) != (e.item().DateLastModified)){

applies the != operator to two different (date) objects. According to the docs:

Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations. (emphasis mine)

These objects may not be 'full' JScript dates - I couldn't call methods on them either - but you'll need to compare their values to avoid unecessary copies. I'd use + to force a comparison of the underlying milliseconds.

Evidence:

// two different Date objects with the same value
var d1 = new Date(1, 2, 3, 4, 5, 6, 7);
var d2 = new Date(1, 2, 3, 4, 5, 6, 7);
WSH.Echo("d1:", d1);
WSH.Echo("d2:", d2);
WSH.Echo("Different objects:");
WSH.Echo("  d1 == d2:", (d1 == d2).toString());
WSH.Echo("  d1 != d2:", (d1 != d2).toString());
WSH.Echo("Same (numerical) value:");
WSH.Echo("  +d1:", +d1);
WSH.Echo("  +d2:", +d2);
WSH.Echo("  d1 - d2:", d1 - d2);
WSH.Echo("  +d1 == +d2:", (+d1 == +d2).toString());

output:

d1: Sun Mar 3 04:05:06 UTC+0100 1901
d2: Sun Mar 3 04:05:06 UTC+0100 1901
Different objects:
  d1 == d2: false
  d1 != d2: true
Same (numerical) value:
  +d1: -2172171293993
  +d2: -2172171293993
  d1 - d2: 0
  +d1 == +d2: true
1
  • Thanks ! that's working :) I'll keep the +d1 trick in mind to do the cast
    – dev.greg
    Oct 17, 2014 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.