5 Replies Latest reply on Dec 13, 2008 5:17 AM by (Dave_Saunders)

    Problem with File.copy() -- should this work?

    Level 1
      I'm trying to copy a number of script files (I'm testing with just one) out of one folder into another. Here's the code:

      (function() {
      
        var myFolder = Folder(getScriptPath().parent + "/disabled");
        if (myFolder.exists) {
          var myScripts = myFolder.getFiles("*.jsx");
            for (var j = 0; myScripts.length > j; j++) {
            if (myScripts[j].copy("../enabled")) {
              myScripts[j].remove();
            }
          }
        }
        function getScriptPath() {
          // This function returns the path to the active script, even when running ESTK
          try {
            return app.activeScript;
          } catch(e) {
            return File(e.fileName);
          }
        } // end getScriptPath
      }())


      What's happening is that the file in the disabled folder is simply being deleted. This means that the copy method is returning true and yet the copy didn't happen. Perhaps my URI is wrong, but if so, the copy should have failed. The file is not in either the InDesign or ESTK packages, which seem to be the only two worth looking in -- I was running from ESTK with InDesign CS4 targeted.

      Dave
        • 1. Re: Problem with File.copy() -- should this work?
          Level 1
          On the plus side, this version works:
          (function() {
          
            var myFolder = Folder(getScriptPath().parent + "/disabled");
            if (myFolder.exists) {
              var myScripts = myFolder.getFiles("*.jsx");
              for (var j = 0; myScripts.length > j; j++) {
                var targ = File(myFolder.parent + "/enabled/" + myScripts[j].name);
                if (myScripts[j].copy(targ)) {
                  myScripts[j].remove();
                }
              }
            }
            function getScriptPath() {
              // This function returns the path to the active script, even when running ESTK
              try {
                return app.activeScript;
              } catch(e) {
                return File(e.fileName);
              }
            } // end getScriptPath
          }())
          I never did feel comfortable with relative paths.

          Dave
          • 2. Re: Problem with File.copy() -- should this work?
            Peter Kahrel Adobe Community Professional & MVP
            Dave,

            "Relative" to what? In your script it's not clear what the current folder is, so "relative" has no meaning here. Besides, the first version of your script also doesn't work because you (try to) copy to a folder, which is not possible. You always need to specify a full path name, as in the second version.

            Peter
            • 3. Re: Problem with File.copy() -- should this work?
              Level 1
              Ah, Thanks Peter.

              I was fooled by the example in the OMV. That just gives ".." as a presumably valid path or why give it.

              Nonetheless, why does the copy() return true when it doesn't work? Or does it interpret a bad path as a request to copy to itself? A far fetched interpretation if you ask me.

              Dave
              • 4. Re: Problem with File.copy() -- should this work?
                Peter Kahrel Adobe Community Professional & MVP
                You can set the current folder like this:

                Folder.current = "/d/test1/test2"

                Then change to that current folder's parent using

                Folder.current = ".."

                But you can't use .. in file masks. This won't work:

                var myFiles = Folder.cuttent.getFiles ("../*.jsx")

                The only way of using .. (as far as I can see) is with literal filenames:

                myFile = '../nonsense.txt'

                which points to nonsense.txt in current folder's parent folder. But this is unclear as you dont't have a reference to the current folder in your statement. I agree that using relative file paths is vague and it looks as if they're best avoided.

                Why copy() returns true when in fact it hasn't worked looks like a bug. I've tried it a couple of times and on Windows it gives the same results as with you: copy() says true when nothing's happened.

                Peter
                • 5. Re: Problem with File.copy() -- should this work?
                  Level 1
                  It never for a second occurred to me that "relative" meant "relative to the current folder" -- I assumed it meant "relative to the current position of the file."

                  It was only because the example in the OMV that I went down this route. And I never even realized that I had used a folder in the first code and a file reference in the second -- I have always used the second code before and I just wrote it out automatically without seeing the inconsistency from the one version to the other.

                  Thanks again.

                  Dave