Search or Filter by file character length

I see there is an option to add a column or use that column to search for files based on path length. However, can something similar be done to search for filename length? For example, when backing up to encrypted folders, files cannot exceed 143 characters in length, so I would like to search and file all those files that do not meet the requirements.

Thanks!

Easily done with a script add-in:

NameLengthColumn.js.txt (717 Bytes)

Drag it to the list of scripts in Preferences / Toolbars / Scripts.

Code in the .txt file for reference:

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "NameLengthColumn";
	initData.version = "1.0";
	initData.copyright = "(c) 2017 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/search-or-filter-by-file-character-length/24611/1
	initData.desc = "Name Lengh Column";
	initData.default_enable = true;
	initData.min_version = "12.0";

	var col = initData.AddColumn();
	col.name = "NameLength";
	col.method = "OnNameLength";
	col.label = "Name Length";
	col.justify = "left";
	col.autogroup = true;
	col.type = "number";
}


// Implement the NameLength column
function OnNameLength(scriptColData)
{
	scriptColData.value = scriptColData.item.name.length;
}
1 Like

Works perfectly, thanks!

To follow up on this, I found out that the encrypted folder accepted file names not specifically 143 character, but actually 143 bytes. although the script is sufficient enough to calculate those bytes based on latin characters (since it's about 1 byte per character), non-English characters take up a lot more bytes usually.

For example, the following test I did with Chinese characters:
汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字字.jpg

The bytes counter (mothereff.in/byte-counter) says it is 51 characters and 145 bytes. The Name length script says it is 21 characters.

I'm not too concerned with the Name Counter script here since it works for my needs with latin characters, but is it possible to do something to count the bytes a name take up, or would that require some 3rd party tool integration?

That depends on the encoding the filesystem uses. On most Windows filesystems it would be utf16/ucs16 and typically just double the number of characters (unless surrogate pairs are involved), but if the filesystem uses utf8 it may be more variable.

Opus provides a StringTools scripting helper which lets scripts encode strings into various encodings, which could be used here.

This version adds a second column, "Name Bytes", which shows the length of the filename encoded as a UTF-8 string.

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
   initData.name = "NameLengthColumn";
   initData.version = "1.0";
   initData.copyright = "(c) 2017 Leo Davidson";
   initData.url = "https://resource.dopus.com/t/search-or-filter-by-file-character-length/24611/1
   initData.desc = "Name Lengh Column";
   initData.default_enable = true;
   initData.min_version = "12.0";

   var col = initData.AddColumn();
   col.name = "NameLength";
   col.method = "OnNameLength";
   col.label = "Name Length";
   col.justify = "left";
   col.autogroup = true;
   col.type = "number";

   var col = initData.AddColumn();
   col.name = "NameBytes";
   col.method = "OnNameBytes";
   col.label = "Name Bytes";
   col.justify = "left";
   col.autogroup = true;
   col.type = "number";
}


// Implement the NameLength column
function OnNameLength(scriptColData)
{
   scriptColData.value = scriptColData.item.name.length;
}

// Implement the NameBytes column
function OnNameBytes(scriptColData)
{
	var tools = DOpus.Create.StringTools();
	var blob = tools.Encode(scriptColData.item.name, "utf-8");
	scriptColData.value = blob.size - 1
}

Yes, Im pretty sure it uses UTF-8 encoding to convert text to bytes. I obviously don't know what I'm doing but I tried some of the StringTools suggestions without any luck getting results.

Option Explicit
Function OnClick(ByRef clickData)

    Dim bytes
    bytes = "abcdefg"
    bytes = DOpus.Create.Blob(byte).size
    DOpus.Output bytes

End Function
Option Explicit
Function OnClick(ByRef clickData)

    Dim Text, StringTools, Strings

    Text = "hfhj"
    Set StringTools = DOpus.Create.StringTools
    Set Strings = StringTools.Decode(Text, "utf-8")
    Dopus.Output Strings.size

End Function

Jon's post above should be all you need.

Oh wow, I can't believe I missed that. You guys are just making this thing do magic!

I was using some really bad workaround with regex [^\x00-\x7F]+ to find non-latin characters in combination with the original name length... can delete this stupid filter now )

Thanks again loe and jon!!