0 Replies Latest reply on Aug 27, 2008 7:31 AM by Andreas Jansson

    Sharing a File / Folder finding with you

    Andreas Jansson Level 2
      I was trying to make a script that created a certain path for me, to make sure it existed.
      Accidentally I had the folder overwritten by a FILE with the same name, and then the auto-check / create folder stopped working. I'll share my findings.
      Perhaps I'm not alone having misinterpreted the documentation and therefore use the new keyword almost by default with the Folder and File objects, just to make sure that you always get the requested object (a Folder or a File). That is not the case.

      // Manually create a file with no extension called testx in the path c:\temp
      var f = new Folder('c:/temp/testx');
      $.writeln('Exists: ' + f.exists);
      // returns true, even though I used the new Folder object and "testx" is actually a file with no extension.
      // This proves that you can actually get a reference to a file, but it's a folder OBJECT we are using for referencing it - obviously that's how one should interpret the statement in the documentation.

      // Ok... the documentation actually states that there are "two ways to distinguish between a File and a Folder object"
      // Let's try them as well, using the same reference:

      if (f instanceof File) $.writeln('f instance is a file.'); // Nope.
      if (f instanceof Folder) $.writeln('f instance is a folder.'); // Yes.

      // The other way was to check whether the open method returned "undefined" (it should do so for folder objects, since they lack the open function).
      if (typeof f.open=='undefined') $.writeln('Does not open = Folder') else $.writeln('Does open = File');
      // This however also returned Folder...

      // But... thinking twice about the statement that the "new" keyword ALWAYS returns a Folder made me test without "new".
      // And removing the "new" keyword from the first row solved everything!