I would like to automatically assign a label ("Ok") to a folder.
The folder contents should be checked.
Only if all text files (and only text files) have the "Ok" label.

I would like to automatically assign a label ("Ok") to a folder.
The folder contents should be checked.
Only if all text files (and only text files) have the "Ok" label.

Install this script: All Text Files OK.opusscriptinstall (1.1 KB)
(Settings > Install Script... - If it's missing due to older toolbars/menus, grab it from the Default Toolbars tab in the Customize dialog.)
The script looks for a label named OK. If you need to change that, go to Settings > Scripts and double-click the script to edit it. The label name is on the first line.
Go to Preferences / Labels / Label Assignments / In Specific Folders
Click Add Folder (or Add Wildcard) and point it at the folder in your screenshot (the one with the other folders below it).
This is highly recommended over making a global label/filter, to avoid performance problems in folders where the label will never apply.
From there, add a Filter Label for that folder:
Define the label like this:
Also change the top-right selection ("Complete" in my example) to choose the label you want the folders to get, i.e. "OK" in your case.
That should be all you need to do.
It only considers text files directly below each folder. Making it recursive is very easy, but I assumed you wouldn't want it to be. Let me know if that's wrong and you need help.
Similarly, folders that don't contain any text files at all will be marked "Not OK", but that's easy to change if you want it to strictly say "are all text files OK, even if there are zero of them".
Script code, for reference (same thing as in the .opusscriptinstall file, above):
var labelName = "OK";
// Called by Directory Opus to initialize the script
function OnInit(initData)
{
initData.name = "All Text Files OK";
initData.version = "1.0";
initData.copyright = "(c) 2024 Leo Davidson";
initData.url = "https://resource.dopus.com/t/folder-label/51678/2";
initData.desc = "Column indicates if all .txt files under a directory have the OK label";
initData.default_enable = true;
initData.min_version = "13.0";
}
// Called to add columns to Opus
function OnAddColumns(addColData)
{
var col = addColData.AddColumn();
col.name = "AllTextOK";
col.method = "OnAllTextOK";
col.label = "All Text OK";
col.justify = "left";
col.autogroup = true;
}
// Implement the AllTextOK column
function OnAllTextOK(scriptColData)
{
if (!scriptColData.item.is_dir)
return;
var anyOK = false;
var allOK = true;
var labelNameUpper = labelName.toUpperCase();
var folderEnum = DOpus.FSUtil.ReadDir(scriptColData.item, "p");
while (!folderEnum.complete)
{
var folderItem = folderEnum.next;
if (!folderItem.is_dir && folderItem.ext.toUpperCase() == ".TXT")
{
var thisOK = false;
var vecLabels = folderItem.Labels("*","explicit");
for (var i = 0; i < vecLabels.size; ++i)
{
DOpus.Output(vecLabels(i));
if (vecLabels(i).toUpperCase() == labelNameUpper)
{
thisOK = true;
anyOK = true;
break;
}
}
if (!thisOK)
{
allOK = false;
break;
}
}
}
scriptColData.value = (anyOK && allOK) ? "OK" : "Not OK";
}
Thank you for a lot of quick work.
Unfortunately it doesn't quite work.

If you turn on the column which the script adds, what does it say for the two folders?
(The column will be in the "Script" category near the bottom of the column list.)

The column seems to be working correctly. My guess is you have a second label assignment somewhere which is affecting the folders as well.
Tested again... new folder, new files...
No other automatic labels.
Column is correct, only label for folder is always displayed.
What happens if you turn off the new label? Do the folder icons still change?
I just tested that...
I can confirm that labels are always apply to folders.
If label is turn off, folder icons are default.
Note : If you display folder with expanded folders then apply label to txt files, script column is not refresh automatically.
nothing helps... tried everything.
Folder label also appears if files in the folder do not have a label or there are no files in the folder.
Including "What happens if you turn off the new label?" If so, what happens?
Sorry, but which labels should I turn off? And how?
do you mean here?
Then the labels on the folders also disappear.

The new label you just added. Using the checkbox next to "All OK" shown on the left of your screenshot here:
Could it be the script?