I would like to be able to sort a folder in the "Source" display by filename, but starting in column 12. I assume it can be done with a script but I'm not very good at that. I would also like to be able to sort ascending or descending. I really appreciate any help I can get. I assume I would create a button and add the script to it.
Thank you... Bob Gregory
Download the script:
Name Substrings.js.txt (830 Bytes)
Then open Preferences / Toolbars / Scripts and drag it to the list of scripts there.
You can then find a Scripts > Name Substring column.
The column will show the filenames, with the first 11 characters removed. (You can edit the script to change the number of characters for other uses.)
Sort by the column as you would a normal one.
Script code (same as in the .js.txt file above):
// Called by Directory Opus to initialize the script
function OnInit(initData)
{
initData.name = "Name Substrings";
initData.version = "1.0";
initData.copyright = "(c) 2017 Leo Davidson";
initData.url = "https://resource.dopus.com/t/sorting-a-portion-of-the-filename/25966";
initData.desc = "Adds a column with substrings of each file's name, for custom sorting.";
initData.default_enable = true;
initData.min_version = "12.0";
var col = initData.AddColumn();
col.name = "NameSubstring";
col.method = "OnNameSubstring";
col.label = "Substring";
col.type = "String";
col.justify = "left";
col.autogroup = true;
col.namerefresh = true;
}
// Implement the OnNameSubstring column
function OnNameSubstring(scriptColData)
{
scriptColData.value = scriptColData.item.name.substring(11);
}
Thanks ever so much Leo. Works great. Is there a way to exclude folders? Sorry I should have put that in my original post
Name Substrings.js.txt (916 Bytes)
function OnNameSubstring(scriptColData)
{
if (scriptColData.item.is_dir)
{
scriptColData.value = "";
}
else
{
scriptColData.value = scriptColData.item.name.substring(11);
}
}
Thank you Leo. Really appreciate it.
I have one more question... promise. Is there a way to NOT display files with an extension of "xxx" or "yyy" or
filenames of "zzzzzzz"? I tried experimenting with your script but got no where.
Thank you again.
Way back in 2017, Leo wrote this script for me. I don't know much about scripting so I was hoping
Leo, or someone with scripting knowledge could modify it. Currently it creates a column with the first 11 characters of the filename removed. It does this for folders and files and I would like it to only affect files. Is this possible?
Thanks,
Bob Gregory
// Called by Directory Opus to initialize the script
function OnInit(initData)
{
initData.name = "Name Substrings";
initData.version = "1.0";
initData.copyright = "(c) 2017 Leo Davidson";
initData.url = "https://resource.dopus.com/t/sorting-a-portion-of-the-filename/25966";
initData.desc = "Adds a column with substrings of each file's name, for custom sorting.";
initData.default_enable = true;
initData.min_version = "12.0";
var col = initData.AddColumn();
col.name = "NameSubstring";
col.method = "OnNameSubstring";
col.label = "Substring";
col.type = "String";
col.justify = "left";
col.autogroup = true;
col.namerefresh = true;
}
// Implement the OnNameSubstring column
function OnNameSubstring(scriptColData)
{
scriptColData.value = scriptColData.item.name.substring(11);
}
Replace
function OnNameSubstring(scriptColData)
{
scriptColData.value = scriptColData.item.name.substring(11);
}
with
function OnNameSubstring(scriptColData)
{
if (scriptColData.item.is_dir) return;
scriptColData.value = scriptColData.item.name.substring(11);
}
Awesome. Thank you Ixp. I will give it a try and let you know the outcome.
Bob
Worked great. Thank you very much for the quick resolution.
Can I also skip certain file names either by using wildcards or the full file name?
It's best to state all your requirements in one post, not drip-feed the details, else people trying to help have to keep going back to the script and re-testing everything.
I totally agree but I didn't know I would need this ability until I ran though different scenarios. Sorry.
Yes. Replace
function OnNameSubstring(scriptColData)
{
if (scriptColData.item.is_dir) return;
scriptColData.value = scriptColData.item.name.substring(11);
}
with
var wld = DOpus.FSUtil().NewWild('*.txt'); // Adapt as needed
function OnNameSubstring(scriptColData)
{
if (scriptColData.item.is_dir) return;
if (!wld.match(scriptColData.item.name)) return; // Remove the ! to invert the logic.
scriptColData.value = scriptColData.item.name.substring(11);
}
Thank you again Ixp. Really appreciated.