5 Replies Latest reply on Nov 26, 2010 4:09 AM by Marijan Tompa [tomaxxi]

    how to Find Modified file

    Mac_06 Level 2

      In folder 3 -4 xml file are there I went to read only latest modified file

      I already find out time of all file but not get the latest file

       

      var myDocument = app.activeDocument;
      var myDocName = myDocument.name;
      var myFolder =myDocument.filePath;
      var Metafilepath=File(myFolder + "/");

          myPath = Metafilepath.getFiles("*.xml");
          if(File(myPath.exists))
          {
            for(var i=0; i<myPath.length; i++)
            {
             Temps = String(myPath[i]);
            
             Tempt1 = Temps.lastIndexOf("/");
             Tempt2 = Temps.substring(Tempt1+1);
             FileName = Tempt2.split(" ");
             var myNewFile = new File(Temps)
             var myModifiedFile = myNewFile.modified
                var myConcat = FileName + " "+ myModifiedFile
             var myNewFile1 = myConcat.toString();     
             myPath[i].close();
             }
          
           }

      Thanks in advance

        • 1. Re: how to Find Modified file
          Marijan Tompa [tomaxxi] Level 4

          Hey!

           

          Here is working snippet:

           

          function findLastFile(fileXT /* file extension */, lastMod){
            var myDocument = app.activeDocument;
            var myFolder = myDocument.filePath;
            var myFiles = myFolder.getFiles("*."+ fileXT);
          
            if(myFiles.length != 0){
              myFiles.sort(function ( a, b )
              {
                if ( a.created < b.created )
                  return -1;
                if ( a.created > b.created )
                  return 1;
                return 0;
              }
              );
              if(lastMod)myFiles.reverse();
              var myFile = myFiles[0];
              return myFile;
            }else{
              return false;
            }
          }
          

           

          And you can use it like this:

           

          // this will give you newest XML
          var myFile = findLastFile("xml", true);
          
          // this will give you oldest XML
          var myFile = findLastFile("xml", false);
          
          // also you can enter different extension
          // this will give you newest InDesign file
          var myFile = findLastFile("indd", true);
          

           

          Hope that helps.

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 2. Re: how to Find Modified file
            Mac_06 Level 2

            Thanks for your quick reply Marijan Thompa

             

            but we are not getting the reference for lastMod

            So i am getting the error "lastMod" is not defined

            Could you eleborate it ?

             

            Many Thanks

            • 3. Re: how to Find Modified file
              Mac_06 Level 2

              Sorry......... for previous mail

              I think, I had asked out of track question

               

              your script did great work with some modification

               

               

              function findLastFile(fileXT /* file extension */, lastMod){

                var myDocument = app.activeDocument;

                var myFolder = myDocument.filePath;

                var myFiles = myFolder.getFiles("*."+ fileXT);

               

                if(myFiles.length != 0){

                  myFiles.sort(function ( a, b )

                  {

                    if ( a.modified < b.modified )

                      return -1;

                    if ( a.modified > b.modified )

                      return 1;

                    return 0;

                  }

                  );

                  if(lastMod)myFiles.reverse();

                  var myFile = myFiles[0];

              //$.write(myFile)

                  return myFile;

                }else{

                  return false;

                }

              }

               

               

              Many Thaks for your great help.

              • 4. Re: how to Find Modified file
                Peter Kahrel Adobe Community Professional & MVP

                You can save yourself some typing:

                 

                myFiles.sort(function ( a, b )  {return a.modified < b.modified});

                 

                Peter

                • 5. Re: how to Find Modified file
                  Marijan Tompa [tomaxxi] Level 4

                  Yes Peter, but I fond this in my old scripts and I used it for sorting by date

                  Thanks!

                   

                  --

                  tomaxxi