Skip to main content
josephb44551697
Known Participant
July 18, 2019
Answered

Loop through all .mif files in a given folder

  • July 18, 2019
  • 2 replies
  • 2496 views

Hello,

I am trying to write a script that does the following:

  1. Asks the user for a folder path.
  2. Loops through all of the .mif files in this folder.
  3. Opens, modifies, and saves each file.

I know how to do 1 and 3, but have no idea how to do #2. I have used the following code to get all files, but can't differentiate mif files.

var myFolder = Folder.selectDialog ("Select a folder");

var files = myFolder.getFiles();

I would appreciate help figuring this out.

Thank you,

Joseph

This topic has been closed for replies.
Correct answer Wiedenmaier

var myFolder = Folder.selectDialog ("Select a folder"); 

var files = myFolder.getFiles("*.mif")

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

{

    var currentFile = files;

    var fileSystemFileName = currentFile.fsName;

    //to something with file object or file name

}

should work like this

Markus

2 replies

WiedenmaierCorrect answer
Inspiring
July 18, 2019

var myFolder = Folder.selectDialog ("Select a folder"); 

var files = myFolder.getFiles("*.mif")

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

{

    var currentFile = files;

    var fileSystemFileName = currentFile.fsName;

    //to something with file object or file name

}

should work like this

Markus

josephb44551697
Known Participant
July 18, 2019

Markus,

That works better than what I just came up with. Thank you!

Joseph

4everJang
Legend
July 18, 2019

Look for the JavaScript Tools Guide. This explains how to use the Folder and File objects.

josephb44551697
Known Participant
July 18, 2019

I just realized that I could turn the filename into a string, then search the end of the string for "mif".

var myFolder = Folder.selectDialog ("Select a folder");

var allFiles = myFolder.getFiles();

var mifFiles = [];

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

{

    var str = allFiles + "";

    if(str.substring(str.length - 3, str.length) == "mif")

    {

        mifFiles.push(allFiles);

    }

}