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

Folder.getFiles Multiple Filters Mask

Guest
Jun 24, 2012 Jun 24, 2012

Copy link to clipboard

Copied

Hi all,

Folder.getFiles (".*") ... in this case it will get all types of files

Folder.getFiles (".jpg") ... in this case it will get all files with .jpg extension

... but how can I filter to get (for example) all files with ".jpg", " .tif" or ".eps" extension ?

ESTK help says: A mask string can also be the name of a function that takes a File or Folder object as its argument. It is called for each file or folder found in the search; if it returns true, the object is added to the return array.

Any idea of how to make such a function?

I will appreciate any help.

TOPICS
Scripting

Views

13.1K

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
Enthusiast ,
Jun 24, 2012 Jun 24, 2012

Copy link to clipboard

Copied

Send an array:

Folder.getFiles (["Files:*.doc;*.docx;*.rtf"])

Edit: That should be:

Folder.getFiles ("Please select files",["Files:*.doc;*.docx;*.rtf"])

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
Enthusiast ,
Jun 25, 2012 Jun 25, 2012

Copy link to clipboard

Copied

Use regex.

Folder.getFiles(/.+\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i);

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 11, 2021 Dec 11, 2021

Copy link to clipboard

Copied

Hi, 

 

Is this work on Macintosh OS?

 

Regards,

-Sumit

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 ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Try it and you'll find out. You're not going to break anything.

 

P.

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 15, 2021 Dec 15, 2021

Copy link to clipboard

Copied

LATEST

Thank you @Peter Kahrel.

Acutally I am windows user but script can be run on both plateform, that's why I asked.

 

Regards,

-Sumit

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
Guru ,
Jun 25, 2012 Jun 25, 2012

Copy link to clipboard

Copied

You can use a regular expression…

var pics = Folder.desktop.getFiles( /\.(epsf?|jpe?g|tiff?)$/i );

$.write( pics.join( '\r' ) );

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 ,
Jun 25, 2012 Jun 25, 2012

Copy link to clipboard

Copied

It depends on your operating system!

From a recent script of mine:

if (File.fs == "Windows")

pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', "Adobe PDF:*.pdf,All files:*.*", false);

else

pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', function(file) { return file instanceof Folder || (!(file.hidden) && (file.name.match(/\.pdf$/i) || file.type == "PDF ")); }, false );

(Note the second part for OS X. Since I know the 'type' of a PDF is "PDF " -- including a space at the end --, this ought to be enough to discern PDFs. However, it's possible the OS File Type might be 'unknown' (for example, with a downloaded file), so I test the extension anyway.)

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 ,
Jul 18, 2013 Jul 18, 2013

Copy link to clipboard

Copied

What would you use to just return the file name and not the full name?

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 ,
Jul 18, 2013 Jul 18, 2013

Copy link to clipboard

Copied

Folder.getFiles(File.name ); but this does not work. Any ideas?

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 ,
Jul 19, 2013 Jul 19, 2013

Copy link to clipboard

Copied

@Gonterman1201 – for me on Mac OSX 10.6.8 it is working like that:

Presume there is a folder named "Test" in the top folder of my internal harddisk and one or some files with suffix "jpg" in it.

var myFolder = Folder("/"+"Test");

var myJPGFilesArray = myFolder.getFiles("*.jpg");

if(myJPGFilesArray !== null){

    for(var n=0; n<myJPGFilesArray.length;n++){

        $.writeln(myJPGFilesArray.name);

        };

    };

It gets all files with suffix "jpg" in the folder "Test"…

getFiles() returns an Array of files and subfolders.

Or null (if there are no files or subfolders)…

Just another example with a filter for a specific file name:

//To filter to a specific file name:

var myFileName = "NewSWFPosterImage.psd";

var myFolder = Folder("/"+"Test");

var myFileArray = myFolder.getFiles(myFileName);

if(myFileArray !== null){

    var myFileFound = myFileArray[0];

    $.writeln(myFileFound.name);

    };

Uwe

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
Enthusiast ,
Aug 25, 2014 Aug 25, 2014

Copy link to clipboard

Copied

@Jongware, your solution for file masking didn't work for me on the MacOS for After Effects.  Do you know of any differences between the two apps?  I'm actually trying to let all file types be selectable in an open dialog.  Any suggestions? 

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 ,
Aug 26, 2014 Aug 26, 2014

Copy link to clipboard

Copied

Arie,

Unfortunately I cannot test with After Effects. This works in InDesign:

Folder.myDocuments.openDlg( 'Load any document', function(file) { return true; }, false );

and I can select any file I want.

If it does not work in AE, then the Javascript Base Classes are not as "shared" across all applications of The Suite as Adobe made us believe.

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 ,
Aug 26, 2014 Aug 26, 2014

Copy link to clipboard

Copied

@Arie – what version of After Effects?
Just tested Jongware's code snippet with After Effects CS5.5 v10.5.0.253 on Mac OSX 10.7.5
Worked as expected.

However running the script from the ESTK on InDesign or After Effects reveals a small difference between the two:

If After Effect's own "New Composition" dialog is open and waiting for user input the script will be executed after the user has answered "New Composition".

Opposed to the behaviour using InDesign: If InDesign's "New Document" dialog is already open and not answered by the user, openDlg will be executed immediately. And if the user of the ESTK chooses a file InDesign will throw an error: "A modal dialog or message is active* " (waiting for user input).

Not so with After Effects. After Effects will wait until the user is answering "New Composition" and then present openDlg().

Uwe

*Roughly translated from my German version.

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
Enthusiast ,
Sep 08, 2014 Sep 08, 2014

Copy link to clipboard

Copied

I was testing against CC.  Jongware's code does in fact work, but I get a strange behavior if I use it on .openDialog() and not .openDlg().   I eventually figured out a method that allowed me to use .openDlg().  Thank you @Jongware for providing the code.  I appreciate it!

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
Explorer ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

For future travelers I was having trouble using a string as my regex expression for Folder.getFiles file masking.

Here's what worked for me:

var FO = new Folder(path);

// Literal

var fileObjs = FO.getFiles(/\.(wav|wma)$/i);

// From String

var mask = "\\.(wav|wma)$";

var re = new RegExp(mask, "i");

var fileObjs = FO.getFiles(re);

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