• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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.

 

TOPICS
How to , Scripting

Views

331

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 01, 2022 Nov 01, 2022

I've had a try at a sorter function that will sort the original file names. Please try this and see if it works for you.

- Mark

 

 

 

fileNames.sort(sortStringByPaddingNumbers);

/**
 * 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) {

    var aParts = a.split(/(\d+)/),
        bPa
...

Votes

Translate

Translate
Community Expert , Nov 01, 2022 Nov 01, 2022

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);
}

/**
 
...

Votes

Translate

Translate
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

thank you for the response @m1b . I will try by changing the file names as you said!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

I've had a try at a sorter function that will sort the original file names. Please try this and see if it works for you.

- Mark

 

 

 

fileNames.sort(sortStringByPaddingNumbers);

/**
 * 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) {

    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;

};

 

Edit: fixed bug in script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

wow, it is working fine! you are really great!

Thanks @m1b 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

Hi @m1b , it is only working for direct sorting of array contents which are strings but when we get the file path that is not an string it was actually a object so getting error! 

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) {
    var aParts = a.split(/(\d+)/),
        bParts = b.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);
    }
    a = aParts.join('');
    b = bParts.join('');
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
};

a is an object and a.name gives the fileName. In this case how to make this function work???

{/d/scripts/20599fig10_1.tif}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

Hi @Karthik SG, use this line for the sort:

filesList.sort(function (a, b) { return sortStringByPaddingNumbers(a.displayName, b.displayName) });

Also the .sortMethod will actually change the array, so you don't need to assign it to a new variable (filesB isn't needed—you can just keep using filesList).

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

I tried like you said also but it was not working for me, please review my code, If any mistake let me know i will change it !

 

var filesB =[];
var myfile=Folder.selectDialog();
var fileList =myfile.getFiles(); 
//filesB =  fileList.sort(sortStringByPaddingNumbers);
filesB = fileList.sort(function (a, b) { return sortStringByPaddingNumbers(a.displayName, b.displayName) });
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;
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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;
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Copy link to clipboard

Copied

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.
 * @author m1b
 * @version 2022-11-01
 * @Param {String} a - a string to order.
 * @Param {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;

};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

LATEST

@m1b as you said when using "Math.max" it throws error in some cases, thanks for the update after changing it to "Math.min" it's working fine now!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines