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

check if a folder is empty

Engaged ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

Greetings to all!
Is there a script that can check if a folder does not contain any files?

 
TOPICS
Actions and scripting

Views

494

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 1 Correct answer

LEGEND , Feb 05, 2021 Feb 05, 2021
Folder('~/desktop').getFiles().length

Votes

Translate

Translate
Adobe
LEGEND ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

Folder('~/desktop').getFiles().length

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 ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

fileExist = Folder("~/desktop/"); fileList = fileExist.getFiles(); 
if(fileList.length == 0){ alert ("Empty folder");} else{ alert ("Non-empty folder"); }

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 ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

Thank you all for the beautiful support!

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
LEGEND ,
Feb 06, 2021 Feb 06, 2021

Copy link to clipboard

Copied

LATEST

Why you're giving the same answer I did 5 hours earlier?

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 ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

Adding to the reply from Kukurykus:

 

 

var howMany = Folder('~/desktop').getFiles().length;
alert(howMany + ' items found');

 

 

It is worth keeping in mind that this will return all files and folders/directories. You may not want to count the folders. Also, just because a file is found, does not mean that it can be processed by Photoshop.

 

It depends on how robust and for what purposes this is being used.

 

 

The same applies to the reply from Shulipa Bernad:

 

 

var fileExist = Folder("~/desktop/");
var fileList = fileExist.getFiles();
if (fileList.length === 0) {
    alert("Empty folder");
} else {
    alert("The folder " + "'" + fileExist + "'" + " contains " + fileList.length + " items.");
}

 

 

To limit the file input, one can do something like this:

 

var fileExist = Folder("~/desktop/");
// Limit file format input & exclude folders from count
var fileList = fileExist.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd)$/i);
if (fileList.length === 0) {
    alert("Empty folder");
} else {
    alert("The folder " + "'" + fileExist + "'" + " contains " + fileList.length + " items.");
}

 

Note: This is not intended as negative critisism, I'm just stating the facts, it all depends on how the code is used.

 

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