5 Replies Latest reply on Feb 23, 2010 8:02 PM by John Hawkinson

    [JS, CS4] file.Copy() results in 0 KB file




      In a nutshell, a script goes through some links, copies the link to a given destination, checks if there's a corresponding .CDR and then copies that to the same destination. It works absolutely perfectly, except for a seemingly random link - when the linked file and CDR are copied, 0 KB copies are created in the destination folder


      This is a snippet of the code, for copying the link (the CDR copy bit is basically the same, just with a bit of string work to get the correct extension):


      function normalLinkCopier(myLinks){
      for (i = 0; i < myLinks.length; i++) {
        var myLink = myLinks[i];
        // Capture the location of the linked file.
        var myLinkFile = new File(myLink.filePath);
        // Capture the destination of the copied link - uses the folder selected previously and the link's name.
        var myNewLinkFile = new File(myFolder + "/" + myLink.name);
      //~   alert ("Original: " + myLinkFile + myLink.size + "\n\nCopy :" + myNewLinkFile)
      // Copy the linked file to the destination



      The alert tells me the correct path and name of the original file (myLinkFile), and the correct path and name of the destination file (myNewLinkFile). The original file exists and it has a size > 0 KB. However, when the copy function runs, it somehow makes the copy 0 KB.


      One time when I was trying it I received this error message:


      IOError: Cannot find the folder "C:\Program Files\Adobe\Adobe InDesign CS4\Scripts\XHTML For Digital Editions\null\<the erroneous file's name was here>.eps


      I have not been able to replicate the above error, however.


      To add to the weirdness, when I delete the link that is causing problems, the problem switches to another seemingly random link (it was links[33] originally, then it switches to links[0]). Delete that, then it switches again...but it's only ever a problem for one link at a time.


      I cannot see what changes between getting the link's path and copying it - and why it only happens on one random link at a time.


      If anyone can shed any light on the situation I will be extremely grateful!

        • 1. Re: [JS, CS4] file.Copy() results in 0 KB file
          [Jongware] Most Valuable Participant

          It really sounds like an internal JS error, rather than something in your code (which, BTW, I didn't test so I may be wrong!).

          This sort of things happen when a lazy programmer uses one global buffer internally to store a single object in, where a program suddenly tries to access it with different values.


          And I bet it's somewhere around the two 'new File(..)' calls.


          You can try this: instead of having one long piece of code, put the stuff inside the for loop into a separate function. Then call this function from the for loop. The idea is that the local variables in a function should be cleaned up correctly right after exiting the function, so they can't interfere with a 'next' occurrence.

          1 person found this helpful
          • 2. Re: [JS, CS4] file.Copy() results in 0 KB file
            Murral Level 1

            I tried putting the loop contents inside a function, then I tried putting the new File lines inside separate functions, but all to no avail. Thanks for the idea though.


            I've discovered something else about this error though. For two links in the document, A and B:


            Select A, run the script, it works fine.

            Select B, run the script, it creates the 0 KB files.

            Select A again, run the script and 0 KB files are created.


            Although I don't know if that opens up any avenues for me to explore...

            • 3. Re: [JS, CS4] file.Copy() results in 0 KB file
              AdobeScripts Level 3

              Murral wrote:


                var myNewLinkFile = new File(myFolder + "/" + myLink.name);

              Worth to try What if you change "/" to "\" ?




              1 person found this helpful
              • 4. Re: [JS, CS4] file.Copy() results in 0 KB file
                Murral Level 1

                A delayed response,  but thanks for your help


                I got the script to work - I copied what InDesign does with its link copier, and that is to skip linked files that already exist in the destination folder. For some reason that solved all of my problems (even though some of the problem files did not already exist in the destination folder).


                And Robin - I copied the code with the forward slash from a colleague's script that works fine, so I didn't even think to check that! File paths seem to work with either slash (I think the error is corrected for you automatically).

                • 5. Re: [JS, CS4] file.Copy() results in 0 KB file
                  John Hawkinson Level 5

                  It's not an error! The File class's API is defined to to accept / as directory seperator on all platforms, for platform independence. It lets you write portable scripts!


                  See, e.g., the Javascript Tools Guide, Chapter 3: File System Access, section Using FIle and Folder objects, subsection Character interpretation in paths:

                  There are some platform differences in how pathnames are interpreted:
                  ➤ On Windows and Mac OS, path names are not case sensitive. In UNIX, paths are case sensitive.
                  ➤ On Windows, both the slash (/) and the backslash (\) are valid path element separators. Backslash is
                  the escape character, so you must use a double backslash (\\) to indicate the character.
                  ➤ On Mac OS, both the slash (/) and the colon (:) are valid path element separators.

                  It's also the case that typing backslashes in JavaScript is awkward and ugly ("\\").