Page 1 of 1

Custome Column With Partial File Name via Script

Posted: 21 Apr 2021 14:46
by kudou
Hello,

I need to generate partial file name(Meter No) from full file name.

I able to do this in Ms Office Excel, need to do the same in xyplorer via script.

Example Scenario:

D_3304711_08042021153020.XML to 3304711

03_2017341_19042021_095157.XML to 2017341

30055094_20122019235900.xml to 30055094

0001_1210344_20210419_125458_187.xml to 1210344

2358530_210421_111135.XML to 2358530

Excel Formula:

=IF((LEN(A2)-LEN(SUBSTITUTE(A2,"D_",""))),SUBSTITUTE(MID(SUBSTITUTE("_"&A2&REPT(" ",6),"_",REPT(",",255)),2*255,255),",",""),IF((LEN(A2)-LEN(SUBSTITUTE(A2,"03_",""))),SUBSTITUTE(MID(SUBSTITUTE("_"&A2&REPT(" ",6),"_",REPT(",",255)),2*255,255),",",""),IF((LEN(A2)-LEN(SUBSTITUTE(A2,"0001_",""))),SUBSTITUTE(MID(SUBSTITUTE("_"&A2&REPT(" ",6),"_",REPT(",",255)),2*255,255),",",""),IF((LEN(A2)-LEN(SUBSTITUTE(A2,"_",""))),TRIM(LEFT(A2,SEARCH("_",A2)-1)),TRIM(LEFT(A2,SEARCH("2021",A2)-1))))))

Filename Example Download Link:

https://we.tl/t-B6x3RncMYH

Thanks In Advance!

Re: Custome Column With Partial File Name via Script

Posted: 21 Apr 2021 14:57
by highend
TLDR;

In other words:

If the string begins with
D_
03_
0001_

use the following token, otherwise the first?

Code: Select all

$base = gpc(<cc_item>, "base");
    if (regexmatches($base, "^(d|03|0001)_")) { return gettoken($base, 2, "_", "t"); }
    return gettoken($base, 1, "_", "t");

Re: Custome Column With Partial File Name via Script

Posted: 22 Apr 2021 05:19
by kudou
Clipboard-20210422.png
Thanks Highend!

Your script works like a charm.

I missed to add one filename scenario:

Need file name before 2021

RSE4655420210402090132982.XML to RSE46554
500720820210404093514.xml to 5007208

Is it possible to copy this custom column file names and also highlight duplicates file name in custom column.

Thank You.

Re: Custome Column With Partial File Name via Script

Posted: 22 Apr 2021 07:37
by highend
Then add that new rule?

Code: Select all

$base = gpc(<cc_item>, "base");
    if (regexmatches($base, "^(d|03|0001)_")) {
        return gettoken($base, 2, "_", "t");
    } elseif (regexmatches($base, "2021")) {
        return gettoken($base, 1, "2021", "t");
    }
    return gettoken($base, 1, "_", "t");
Copy to clipboard

Code: Select all

$ccCaption = "Test"; // Change this to the name of your cc column
    $ccNames = report("{$ccCaption}<crlf>", 1); // Switch it to 0 for all names (not only the selected ones)
    copytext $ccNames;
Select duplicates (I won't highlight them via tagging because you would need to remove them at some point afterwards)

Code: Select all

$ccCaption = "Test"; // Change this to the name of your cc column
    $ccNames = report("{$ccCaption}|{fullname}<crlf>", 0);
    $ccNames = formatlist($ccNames, "en", <crlf>);

    $duplicates = "";
    while (true) {
        if (!$ccNames) { break; }

        $item    = gettoken($ccNames, 1, <crlf>);
        $ccPart  = gettoken($item, 1, "|");
        $matches = regexmatches($ccNames, "^$ccPart\|.+?$", <crlf>);

        if (gettoken($matches, "count", <crlf>) >= 2) {
            // Remove first item from it (that's the "original")
            $highlights  = gettoken($matches, 2, <crlf>, , 2);
            $duplicates .= regexreplace($highlights, "^.+\|") . <crlf>;
        }
        // Remove from our current list to process
        $ccNames = formatlist($ccNames, "F", <crlf>, "!" . $matches);
    }
    if ($duplicates) {
        $duplicates = trim($duplicates, <crlf>);
        selectitems $duplicates;
    }

Re: Custome Column With Partial File Name via Script

Posted: 22 Apr 2021 09:05
by kudou
Thanks Highend you saved my day!

One problem with filename generating

When there is no "Hyphen" in filename need the meter no before 2021

For this

60080420210402094347.xml to 600804

This is need to be

4437322_MR124_22_BILL_2021_04_01_15_44_05_602f78e26693075cec13e0d1_020421_065013.OUT.XML to 4437322
4437322_MR124_22_BILL_2021_04_01_15_44_05_602f78e26693075cec13e0d1_030421_221018.OUT.XML to 4437322
10185734_MR124_22_BILL_2021_04_01_15_59_47_602f78e06693075cec13e091_020421_214316.OUT.XML to 10185734


Thank You!

Re: Custome Column With Partial File Name via Script

Posted: 22 Apr 2021 09:28
by highend
60080420210402094347.xml to 600804
That was already covered

Code: Select all

$base = gpc(<cc_item>, "base");
    if (regexmatches($base, "^(d|03|0001)_")) {
        return gettoken($base, 2, "_", "t");
    } elseif (strpos($base, "_MR124_") != -1) {
        return gettoken($base, 1, "_", "t");
    } elseif (strpos($base, "2021") != -1) {
        return gettoken($base, 1, "2021", "t");
    }
    return gettoken($base, 1, "_", "t");
I won't add any more rules...

Re: Custome Column With Partial File Name via Script

Posted: 22 Apr 2021 09:36
by kudou
Thanks Highend!

Its Working Now.