2 Replies Latest reply on Mar 9, 2016 6:30 AM by SuperMerlin

    Process files within a folder, in numerical order and not alphabetical

    dereka45241259

      I am using a script to print documents within a folder.

       

      The documents are named 1,2,3,4,5 etc...

       

      However, lets say there is a folder containg 1.jpg, 2.jpg..... 10.jpg, 11.jpg etc...

       

      It seems photoshop's native way of handling this would be to process them in this order:

       

      1.jpg

      10.jpg

      11.jpg

      etc...

      2.jpg

      20.jpg

      21.jpg

      etc....

       

      Is there a line of code that I can add into my script to make it process them like this:

       

      1.jpg

      2.jpg

      3.jpg

      4.jpg

      etc...

      10.jpg

      11.jpg

      etc...

        • 1. Re: Process files within a folder, in numerical order and not alphabetical
          c.pfaffenbichler Level 9

          How about posting a Script when you are talking about it?

          • 2. Re: Process files within a folder, in numerical order and not alphabetical
            SuperMerlin Level 5
            #target photoshop;
            var inputFolder= Folder.selectDialog ("Please select folder to process");
            if(inputFolder != null){
            var files = inputFolder.getFiles("*.jpg");
            files.sort(sortAlphaNum);
            //display first 10 files
            alert(files.splice(0,10).join('\n'));
            }
            
            function sortAlphaNum(a, b) {
                var x = a.toString().split("/"); 
                var y = b.toString().split("/");
                x = x[x.length-1].replace(/\\\s/g," ").split(/(\d+)/); 
                y = y[y.length-1].replace(/\\\s/g," ").split(/(\d+)/);
                for (var i in x) {
                    if (x[i] && !y[i] || isFinite(x[i]) && !isFinite(y[i])) {
                        return -1;
                    } else if (!x[i] && y[i] || !isFinite(y[i]) && isFinite(y[i])) {
                        return 1;
                    } else if (!isFinite(x[i]) && !isFinite(y[i])) {
                        x[i] = x[i].toLowerCase();
                        y[i] = y[i].toLowerCase();
                        if (x[i] < y[i]) return -1;
                        if (x[i] > y[i]) return 1;
                    } else {
                        x[i] = parseFloat(x[i]);
                        y[i] = parseFloat(y[i]);
                        if (x[i] < y[i]) return -1;
                        if (x[i] > y[i]) return 1;
                    }
                }
                return 0;
            };