Skip to main content
josephb44551697
Known Participant
Jul 18, 2019 7:44 PM UTC
Answered

Loop through all .mif files in a given folder

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 is closed to new replies. Start a new post to keep the conversation going.
WiedenmaierCorrect answer
Inspiring
Jul 18, 2019 8:00 PM UTC

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

4 replies

WiedenmaierCorrect answer
Inspiring
Jul 18, 2019 8:00 PM UTC

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

Translate
josephb44551697
Known Participant
Jul 18, 2019 8:02 PM UTC

Markus,

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

Joseph

Translate
4everJang
Legend
Jul 18, 2019 7:57 PM UTC

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

Translate
josephb44551697
Known Participant
Jul 18, 2019 8:01 PM UTC

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

    }

}

Translate