Labeling folders gray if blank

I'm having trouble labeling folders gray when they are blank. I have used the thread linked below for guidance, in which I modified the script to ignore files that mess with the folder size, in this case "desktop.ini".

I'm running into one small issue where the main sub folder will not show up gray if there are multiple subfolders that are also technically empty that contain desktop.ini only because I would assume the script only looks through the main folder and not the subfolders. I can add a field to also look for the size, but if I have text files or other small documents it could be inaccurate. How can I make sure that they all show up gray.

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "EmptyDirExceptPart";
	initData.version = "1.0";
	initData.copyright = "(c) 2017 jpotter";
//	initData.url = "https://resource.dopus.com/viewforum.php?f=35";
	initData.desc = "";
	initData.default_enable = true;
	initData.min_version = "12.0";

	var col = initData.AddColumn();
	col.name = "EmptyDirExceptPart";
	col.method = "OnEmptyDirExceptPart";
	col.label = "EmptyDirExceptPart";
	col.justify = "left";
	col.autogroup = true;
}

// Implement the EmptyDirExceptPart column
function OnEmptyDirExceptPart(scriptColData)
{
	if (!scriptColData.item.is_dir) return;
	var readDir = DOpus.FSUtil.ReadDir(scriptColData.item);
	while (!readDir.complete) {
		var item = readDir.Next()
		if (/*item.is_dir || */item.name != "desktop.ini")
		{
			scriptColData.value = "0";
			return;
		}
	}
	readDir.Close();
	scriptColData.value = "1";
}

Yes, it does.

Change

var readDir = DOpus.FSUtil.ReadDir(scriptColData.item);

to

var readDir = DOpus.FSUtil.ReadDir(scriptColData.item, 'r');

to make it loop through all subfolders as well.

I would also move the Script Column clause to the bottom of the list, so it is checked last, as launching a script will take longer than the other two clauses and there's no point launching the script for things that aren't folders.

So it doesn't appear to be working. It looks like if there are any subfolders within the folder, that folder doesn't work. How would you ignore the subfolders if the subfolders are empty. If it contains desktop.ini it should be considered empty.

Refer to the image below, folders "test" and "test2" should also be gray.
Clipboard Image

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "EmptyDirExceptPart";
	initData.version = "1.0";
	initData.copyright = "(c) 2017 jpotter";
//	initData.url = "https://resource.dopus.com/viewforum.php?f=35";
	initData.desc = "";
	initData.default_enable = true;
	initData.min_version = "12.0";

	var col = initData.AddColumn();
	col.name = "EmptyDirExceptPart";
	col.method = "OnEmptyDirExceptPart";
	col.label = "EmptyDirExceptPart";
	col.justify = "left";
	col.autogroup = true;

}

// Implement the EmptyDirExceptPart column
function OnEmptyDirExceptPart(scriptColData)
{
	if (!scriptColData.item.is_dir) return;
	var readDir = DOpus.FSUtil.ReadDir(scriptColData.item, 'r');
	while (!readDir.complete) {
		var item = readDir.Next()
		if (item.is_dir || item.name != "desktop.ini")
		{
			scriptColData.value = "0";
			return;
		}
	}
	readDir.Close();
	scriptColData.value = "1";
}

This may not be what you need for your project, but...

Remote Empty Directories (Remove Empty Directories (aka RED))

I have a button on my DOpus button bar to call this program.

Try

// Called by Directory Opus to initialize the script
function OnInit(initData) {
    initData.name = 'EmptyDirExceptPart';
    initData.version = '1.0';
    initData.copyright = '(c) 2017 jpotter';
    //	initData.url = "https://resource.dopus.com/viewforum.php?f=35";
    initData.desc = '';
    initData.default_enable = true;
    initData.min_version = '12.0';
    var col = initData.AddColumn();
    col.name = 'EmptyDirExceptPart';
    col.method = 'OnEmptyDirExceptPart';
    col.label = 'EmptyDirExceptPart';
    col.justify = 'left';
    col.autogroup = true;
}
// Implement the EmptyDirExceptPart column
function OnEmptyDirExceptPart(scriptColData) {
    if (!scriptColData.item.is_dir) return;
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var readDir = DOpus.FSUtil.ReadDir(scriptColData.item, 'r');
    while (!readDir.complete) {
        var item = readDir.Next();
        if (item.is_dir && fso.GetFolder(item).size == 0) continue;
        if (item.name != 'desktop.ini') {
            scriptColData.value = '0';
            return;
        }
    }
    readDir.Close();
    scriptColData.value = '1';
}

Only lightly tested, though :blush:

Almost. When there is a subfolder within a folder and it contains desktop.ini, it still doesn't work. It does work if the subfolder doesn't have desktop.ini

Clipboard Image

If this doesn't work, please post a test case and the label definition.

// Called by Directory Opus to initialize the script
function OnInit(initData) {
    initData.name = 'EmptyDirExceptPart';
    initData.version = '1.0';
    initData.copyright = '(c) 2017 jpotter';
    //	initData.url = "https://resource.dopus.com/viewforum.php?f=35";
    initData.desc = '';
    initData.default_enable = true;
    initData.min_version = '12.0';
    var col = initData.AddColumn();
    col.name = 'EmptyDirExceptPart';
    col.method = 'OnEmptyDirExceptPart';
    col.label = 'EmptyDirExceptPart';
    col.justify = 'left';
    col.autogroup = true;
}
// Implement the EmptyDirExceptPart column
function OnEmptyDirExceptPart(scriptColData) {
    if (!scriptColData.item.is_dir) return;
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var readDir = DOpus.FSUtil.ReadDir(scriptColData.item, 'r');
    while (!readDir.complete) {
        var item = readDir.Next();
        if (item.is_dir) {
            if (fso.GetFolder(item).size == 0) continue;
        } else {
            if (item.name != 'desktop.ini') {
                scriptColData.value = '0';
                return;
            }
        }
    }
    readDir.Close();
    scriptColData.value = '1';
}
1 Like

Great work!. Thank you for the help

Clipboard Image

I just started receiving an error where the filter doesn't work.

Error on

 7/16/2021 6:03 AM EmptyDirExceptINI:  Error at line 25, position 13
 7/16/2021 6:03 AM EmptyDirExceptINI:  Permission denied (0x800a0046)

Looks like you are examining parts of your computer that they don't want you to see :wink:

Would be surprised if this was a regular folder.

Weird, not sure what that means, but it does this in any folder on any drive.

It looks like it ends up turning gray eventually, but it takes long if there is an error.

There are folders like System Volume Information in the root of most drives that can't be accessed. It might be trying to look inside one of those.

You really don't want a label like that to be applied everywhere though. It will create a lot of unnecessary SSD/HDD access every time you change folders, which is bad for performance.

Restrict it to the folders you need it in, and where there is only a shallow set of sub-folders to test, and put the path restriction as the first clause in the filter (before the Script Column, so it doesn't run the script in places it doesn't need to).

Alternatively, remove it from the global labels list and add it to the Labels tab of a Folder Format which matches the paths you need it in.

1 Like

Thank you. I set up (2) separate conditions.

  • (1) with the script that only checks my data and thumb drive
  • and the second condition that will not use the script but will check the folder size.

Hopefully that is what you meant, but it seems to be working well.


The "Empty folders everywhere" will also slow things down a little, although a lot less. Up to you, but I'd generally recommend avoiding that kind of filter outside of places where you really need it.

You can push Ctrl-K (Edit > Calculate Folder Sizes) to quickly calculate folder sizes and find empty folders when needed in random folders. Although empty folders don't cause problems so they aren't something to worry too much about.

Sounds good. I'll remove that one. Thanks for your help, Leo