Skip to main content
Inspiring
November 1, 2022
Answered

How to get filesNames as it was in selected folder structure in indesign javascript?

  • November 1, 2022
  • 1 reply
  • 985 views
var filesB =[];
var myfile=Folder.selectDialog();
var fileList =myfile.getFiles(); 
filesB =  fileList.sort();
for (i = 0; i < filesB.length; i++) { 
    $.writeln(filesB[i]);
 }

Why I am unable get actual file order present in selected folder?

or 

How to sort final array I got from the folder?

 

Actually i tried to get files from selected folder to new array but I am unable to get the same order it was listed in my browsing drive folder .If  there is any method to get as it was please let me know.

 

This topic has been closed for replies.
Correct answer m1b

I tried by converting the arguments name to string after that change, this is also working fine @m1b !

var filesB =[];
var myfile=Folder.selectDialog();
var fileList =myfile.getFiles(); 
filesB =  fileList.sort(sortStringByPaddingNumbers);
for (i = 0; i < filesB.length; i++) { 
    $.writeln(filesB[i]);
 }

/**
 * Sorter function that adds
 * leading zeros to all numbers
 * so they sort better.
 *  m1b
 *  2022-11-01
 *  {String} a - a string to order.
 *  {String} b - a string to order.
 *  {Number} - sort result (-1, 0, or 1).
 */
function sortStringByPaddingNumbers(a, b) {
    c = String(a.name);
    d=  String(b.name);
    var aParts = c.split(/(\d+)/),
        bParts = d.split(/(\d+)/),
        len = Math.max(aParts.length, bParts.length);
    for (var i = 0; i < len; i++) {
        if (aParts[i].length === bParts[i].length)
            continue;
        var an = Number(aParts[i]),
            bn = Number(bParts[i]);
        if (an === an)
            aParts[i] = ('0000000000' + aParts[i]).slice(-10);
        if (bn === bn)
            bParts[i] = ('0000000000' + bParts[i]).slice(-10);
    }
    c = aParts.join('');
    d = bParts.join('');
    if (c < d) return -1;
    if (c > d) return 1;
    return 0;
};

 


Hey, very well done on fixing it! For information, here is my preferred way below. Also note I made a mistake on the sorter function: please change Math.max to Math.min. This will throw error in some cases.

- Mark

var myfile = Folder.selectDialog();
if (myfile) {
    var fileList = myfile.getFiles();
    fileList.sort(function (a, b) { return sortStringByPaddingNumbers(a.displayName, b.displayName) });
    for (i = 0; i < fileList.length; i++)
        $.writeln(fileList[i].displayName);
}

/**
 * Sorter function that adds
 * leading zeros to all numbers
 * so they sort better.
 * @7111211 m1b
 * @version 2022-11-01
 * @9397041 {String} a - a string to order.
 * @9397041 {String} b - a string to order.
 * @Returns {Number} - sort result (-1, 0, or 1).
 */
function sortStringByPaddingNumbers(a, b) {

    var aParts = a.split(/(\d+)/),
        bParts = b.split(/(\d+)/),
        len = Math.min(aParts.length, bParts.length);

    for (var i = 0; i < len; i++) {

        if (aParts[i].length === bParts[i].length)
            continue;

        var an = Number(aParts[i]),
            bn = Number(bParts[i]);

        if (an === an)
            aParts[i] = ('0000000000' + aParts[i]).slice(-10);

        if (bn === bn)
            bParts[i] = ('0000000000' + bParts[i]).slice(-10);

    }

    a = aParts.join('');
    b = bParts.join('');

    if (a < b) return -1;
    if (a > b) return 1;
    return 0;

};

 

1 reply

m1b
Community Expert
Community Expert
November 1, 2022

Hi @Karthik SG, I don't know of any way to "get the same order it was listed in [your] browsing drive folder", but you can sort the list of files based on any of their properties. See File's properties. For example:

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

 - Mark

Inspiring
November 1, 2022

Hi @m1b , thanks for your response!

I tried to sort it out already but it is also not helping in my case.

 var files = ["2fig10_1.tif","2fig11_1.tif","2fig3_1.tif","2fig3_2.tif","2fig4_1.tif","2fig5_1.tif","2fig6_1.tif","2fig6_2.tif","2fig7_1.tif","2fig8_1.tif","2fig9_1.tif","2fig9_2.tif","2map13_1.eps","2map1_1.tif","2map2_1.eps"]
 //var newlist = files.sort();
//var newlist =  files.sort(function(a, b){return b-a});
var newlist =files.sort(function (a, b) { return b.modified - a.modified });
 for(var a=0; a<newlist.length;a++){
 $.writeln(newlist[a]); 
 }

for your easy reference i converted the myfolder fileNames into an array! Please help me to sort it out in ascending order sequence!

m1b
Community Expert
Community Expert
November 1, 2022

Oh I see. One confusion is that you called the array "files" but they aren't files; better to call the array "fileNames".

The main problem is that the file names don't use numerical padding so they won't sort the way you want. For example this is a correct sort of the current file names:

2fig10_1.tif
2fig11_1.tif
2fig3_1.tif
2fig3_2.tif
2fig4_1.tif
2fig5_1.tif
2fig6_1.tif
2fig6_2.tif
2fig7_1.tif
2fig8_1.tif
2fig9_1.tif
2fig9_2.tif
2map13_1.eps
2map1_1.tif
2map2_1.eps

But you probably want something like this:

2fig03_1.tif
2fig03_2.tif
2fig04_1.tif
2fig05_1.tif
2fig06_1.tif
2fig06_2.tif
2fig07_1.tif
2fig08_1.tif
2fig09_1.tif
2fig09_2.tif
2fig10_1.tif
2fig11_1.tif
2map01_1.tif
2map02_1.eps
2map13_1.eps

If the file names were padding with leading zeros, it works. Can you rename the files?

- Mark