5 Replies Latest reply on Dec 13, 2016 10:39 AM by williamadowling

    Deleting file from Hot Folder

    Babymac08

      I have a script that is running a series of commands triggered from a file in a hot folder...

       

      I'm trying to delete the original file from the IN folder once the script is done...

       

      What' I've tried is not working... Any Ideas...

       

      var delfo = Folder ( '~/Desktop/Illustrator Hot Folder/11 x 8.5 Proof/In' ); 

      var delfiles = delfo.getFiles (); 

       

      delfiles.remove();

        • 1. Re: Deleting file from Hot Folder
          Silly-V Adobe Community Professional

          When you get an array of files with getFiles(), you may have to go with a loop over them all and remove each one.

          I do it backwards so that it doesn't mess up when files are removed and the array length changes.

           

            var myFolder = Folder.selectDialog();

            var myFiles = myFolder.getFiles();

            for(var i=myFiles.length - 1; i > -1;  i--){

              myFiles[i].remove();

            };

          • 2. Re: Deleting file from Hot Folder
            Babymac08 Level 1

            Silly-V  thanks so much.. I'll give this a try... When you get a second... can you help me understand better what the commands in the "for" line are doing... I'm getting tripped up every time on that section... I've been able to understand and learn several of the other steps and how "var" are used to reference commands... but the various characters like you shown are still stumping me...

             

            Thanks

            • 3. Re: Deleting file from Hot Folder
              williamadowling Level 4

              Here's a breakdown. Please don't be offended if this explanation insults your intelligence. That wasn't my intention. i assumed zero knowledge so that you could get whatever info you needed, and skip over anything you know already.

               

              the anatomy of a for loop:

               

              "for" is the command that identifies you're about to declare a for loop.

               

              Your loop "instructions" are wrapped in (parentheses)

               

              There are typically 3 elements in a for loop, separated by semicolons.

               

              the first is the loop variable definition: var i = myFiles.length -1

              This is saying, set the variable i to the value of myFiles.length and then subtract one

              (you need to subtract one because you'll be accessing the files by their index, and JS uses zero based indexes. that means that if you have 9 files and you try to access myFiles[9] you'll get an error because that index doesn't exist. the 9th file is myFiles[8])

               

              the next element is the loop condition: i > -1

              this part is essentially a "while loop". It means, while the variable i is greater than -1, execute the code inside the loop.

               

              the last element is (i'm sure there's an official name for it, but i'm not sure what it is) an "incrementor" or "decrementor".

              This element defines how your loop variable should behave when the loop has finished executing.

              In Silly-V's example above, you see: i--

              This is shorthand syntax for: i = i - 1

              In other words, take the existing value of i and subtract one each time the loop finishes executing.

               

              Finally, inside the curly brackets is the actual code you want to execute each time through the loop.

              In Silly-V's example, he is saying "hey. get me the item from myFiles that has the index i and remove it."

              the getFiles() method returns an array of items, so you can access each item by index which is what's denoted by the square brackets.

               

              Let's take a look at a really simple example, and i think it will help you understand what's going on during the execution of the loop.

               

              //first we'll create an array of items to work with

              var fruits = ["orange", "grapes", "apple"];

               

              //now lets run a loop that executes some code one time for each element of the fruits array

              //i'm going to use a different loop variable than i since it can be hard to read and confused with l or 1

              //you can set the loop variable to virtually anything you want, as long as it doesn't start with a number.

              //I'll use "a"

              for ( var a = 0; a < fruits.length; a++ )

              {

                   alert( fruits[a] );

              }

               

              so I've set the loop variable "a"  equal to zero.

              I've instructed the loop to execute while "a" is less than fruits.length

              and each time the loop completes, i want to add one to "a"

               

              The first time through the loop, the code is going to find the value of fruits[a]. Since a = 0, the statement effectively says this:

              alert( fruits[0] );

              Remember that arrays are 0 indexed. so 0 means the first element. You'll get an alert that contains the text from the first element of the fruits array, which is "orange".

               

              When the alert is closed, "a" will be incremented, and it will now be equal to 1.

              The second time through the loop, a = 1, so you will get an alert that says "grapes" since that's the second element in the fruits array.

               

              etc.

               

              Copy that code into ExtendScript ToolKit and play around with it until you feel you know what's going on. Add items to the array or rearrange them so you can see what happens when they change.

               

              Final note: you probably noticed that there was a difference between my example and Silly-V's. My example runs "forwards" and Silly-V's example runs "Backwards". Here's the difference. consider how my example looks both forwards and backwards:

              for (var a=0; a < fruits.length; a++) //this is forwards
              for (var a = fruits.length -1; a > -1; a--) //this is backwards

               

              notice that in the backwards example I had to subtract 1 from the fruits.length when setting the loop variable, but in the forwards example, I did not. That's because in the forward example, i said "while a is less than fruits.length". So whenever a becomes equal to fruits.length, then the loop is no longer valid and execution will continue on to whatever comes after that loop.

              • 4. Re: Deleting file from Hot Folder
                Babymac08 Level 1

                This is an amazing help.. .and so many lightbulbs came on while reading this... You have just helped open so many doors for me in scripting...

                 

                I really appreciate all the help you've provided over the last couple of weeks... I have been able to successfully build my Proof Template script I wanted... and now I'm trying to find more ways to automate or simplify the procedure even deeper... But none of it would have been possible without the help of so many in the Scripting Forum...

                 

                My deepest Gratitudes...

                • 5. Re: Deleting file from Hot Folder
                  williamadowling Level 4

                  Glad we could help. I guess Silly-V was right. you will inevitably be, "ONE OF US. ONE OF US. (come on everybody, say it with me) ONE OF US!"

                   

                  Haha. It truly is amazing what can be done with the right tools and procedures. Sooner rather than later you're going to start blowing your colleagues and bosses away by how much easier and faster you're able to make things. =)