7 Replies Latest reply on Feb 1, 2014 4:15 PM by Colin Flashman

    Help

    creativejoan0425 Level 1

      I have a folder contain 2 indd files and one txt file

      I want to do a script if the folder contain a txt file then alert("a") one time;

      if folder not contain txt file do the other things alert("b") one time;

      Here is my script, but I found that if i have txt file, it will alert 5 times (bbabb)

      and if I didn't have txt file, it will alert 4 times (b)

       

      Test();

      function Test(){

      var myDoc = app.activeDocument;

      var DocFilePath = myDoc.filePath;

      var folder_Path = Folder(DocFilePath);

      var sourceChildrenArr = folder_Path.getFiles();

      for (var i = 0; i < sourceChildrenArr.length; i++) {

              var sourceChild = sourceChildrenArr[i];

          if (sourceChild instanceof File && sourceChild.name.match(/\.txt$/i)) {

              alert("a");

          }

           else{

               alert("b");

      }

      }

      }

        • 1. Re: Help
          Mi_D Level 3

          remove 1st two line like below

          Test();

          function Test(){

           

           

          var folder_Path = new Folder("E:\\Dummy\\test");

          var sourceChildrenArr = folder_Path.getFiles();

          for (var i = 0; i < sourceChildrenArr.length; i++) {

                  var sourceChild = sourceChildrenArr[i];

              if (sourceChild instanceof File && sourceChild.name.match(/\.txt$/i)) {

                  alert("a");

              }

               else{

                   alert("b");

          }

          }

          }

           

          in your code it's alert 5 time because it count two indd history file.

           

          And if u went in active Document tha in case of else you use else if like below

           

          Test();

          function Test(){

          var myDoc = app.activeDocument;

          var DocFilePath = myDoc.filePath;

          var folder_Path = Folder(DocFilePath);

          var sourceChildrenArr = folder_Path.getFiles();

          for (var i = 0; i < sourceChildrenArr.length; i++) {

                  var sourceChild = sourceChildrenArr[i];

              if (sourceChild instanceof File && sourceChild.name.match(/\.txt$/i)) {

                  alert("a");

              }

              else   if (sourceChild instanceof File && sourceChild.name.match(/\.indd$/i)){

                   alert("b");

          }

          }

          }

           

          Mi_D

          • 2. Re: Help
            creativejoan0425 Level 1

            I try the second script, i use else if to instead of else

             

            but the result when my folder only have 2 indd files, it will alert("b") twice times, can fix it to be 1 time?

             

            if my folder contain 2 indd files and one txt file, it will alert 3 times (bba), can fix it to alert("a") 1 time?

            • 3. Re: Help
              creativejoan0425 Level 1

              and i try delete the else then it will show one time alert("a")

               

              Test();

              function Test(){

              var myDoc = app.activeDocument;

              var DocFilePath = myDoc.filePath;

              var folder_Path = Folder(DocFilePath);

              var sourceChildrenArr = folder_Path.getFiles();

              for (var i = 0; i < sourceChildrenArr.length; i++) {

                      var sourceChild = sourceChildrenArr[i];

                  if (sourceChild instanceof File && sourceChild.name.match(/\.txt$/i)) {

                      alert("a");

                  }

              }

              }

              • 4. Re: Help
                Laubender Adobe Community Professional & MVP

                @creativejoan0425 – or use a counter in the if statement and evaluate the result of that counter after the for loop.

                 

                Test();
                
                function Test(){
                
                var myCounter = 0;
                
                var myDoc = app.activeDocument;
                var DocFilePath = myDoc.filePath;
                
                var folder_Path = Folder(DocFilePath);
                
                var sourceChildrenArr = folder_Path.getFiles();
                
                for (var i = 0; i < sourceChildrenArr.length; i++) {
                
                    var sourceChild = sourceChildrenArr[i];
                
                    if (sourceChild instanceof File && sourceChild.name.match(/\.txt$/i)) {
                
                        // myCounter = myCounter + 1;
                        // short form:
                        ++myCounter;
                
                        };
                
                    };
                
                if(myCounter === 0){alert("b")};
                if(myCounter === 1){alert("a")};
                if(myCounter > 1){alert( "not a or b:"+"\t"+myCounter )};
                
                }; //END function Test()
                

                 

                Uwe

                 

                //EDIT: the snippet used ".log" suffix; changed that to ".txt".

                 

                Message was edited by: Laubender

                • 5. Re: Help
                  creativejoan0425 Level 1

                  Thx, it works

                  • 6. Re: Help
                    creativejoan0425 Level 1

                    I want to ask what diffent between =, ==, ===?