2 Replies Latest reply on Mar 8, 2010 2:17 PM by TestOCM

    [JS] Date Variable in File Path

    TestOCM

      Hello,

       

      I have simple script that is placing files on to the page.

       

       

       

      var myDate = new Date();

      var myDay=myDate.getDate();

      var myMonth=myDate.getMonth() + 1;

      var myYear=myDate.getFullYear();

      var myTotal = myDay + "/" + myMonth + "/" + myYear;

       

       

       

      var myDoc = app.activeDocument;

      var afile = File("/Files/max/Desktop/'myTotal'_name.txt") >>>>Could please someone suggest how I can use variable myTotal - as prefix for the name of the file ? I need to open files with actual date

      var bfile = File("/Files/max/Desktop/b.txt")

      var cfile = File("/Files/Max/Desktop/c.txt")

      if (!afile.exists || !bfile.exists || !cfile.exists)

      {

      alert ("Text file missing...")

      }

      else

      {

      myDoc.textFrames.item("a").parentStory.insertionPoints.item(-1).place(afile);

      myDoc.textFrames.item("b").parentStory.insertionPoints.item(-1).place(bfile);

      myDoc.textFrames.item("c").parentStory.insertionPoints.item(-1).place(cfile);

      }

       

       

      Thank you

        • 1. Re: [JS] Date Variable in File Path
          [Jongware] Most Valuable Participant

          The argument to File(...) is a simple text string, and simple text strings are concatenated using the + operator:

           

          a = "b";

          b = "c";

          c = a+b; -> c = "bc";

           

          You are complicating things by mixing both single and double quotes. Javascript accepts either, but the same ones must be used for both opening and closing a string.

           

          Instead of

           

          ("/Files/max/Desktop/'myTotal'_name.txt"

           

          use

           

          ("/Files/max/Desktop/"+myTotal+"_name.txt")

           

          By the way, as you can see in your own File(..) line, the forward slash is defined as a path separator. It's possible you have a folder structure "Files", containing "max", containing "Desktop", but since you add 'myTotal' to the mix -- which also contains forward slashes -- you have to ensure all path elements in that exist as well.

           

          (As it seems you were not intending that, I'd suggest using other date separators than forward -- or backward! -- slashes.)

          • 2. Re: [JS] Date Variable in File Path
            TestOCM Level 1

            Thank You that was easy....

            I will take your suggestions ...

             

            I did change slashes to underscore in date...

             

            CHeers