RegExp Rename modify basename and preserve extension

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

RegExp Rename modify basename and preserve extension

Post by yusef88 »

Can regex keep this values in the basename only and also keep the extension?
[^\u0600-\u06FF|0-9|-|_|\s] >
2021-12-10_045823.png
I have another script that seems to work but I'm asking how I can keep the extension while changing what I want in the base name?

Code: Select all

    $a = get(SelectedItemsNames, "<crlf>");
    $a = regexmatches("$a", "-|_|\s|[\u0600-\u06FF|0-9]|(\.[^.]+$)", "");
    $a = RegExReplace("$a", "( ) +(?![ .])| +(?=\.)", "$1");
    rename "l", "$a", p, , 64;
in PowerShell
Get-ChildItem | Rename-Item -NewName {($_.BaseName -replace '([^\u0600-\u06FF|0-9|\s|-|_])','') + $_.Extension}
To see the attached files, you need to log into the forum.

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: RegExp Rename modify basename and preserve extension

Post by highend »

01. Paste the real names (as TEXT) of the three files here
02. Paste the expected result (again as TEXT, after the renaming operation) here as well^^
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: RegExp Rename modify basename and preserve extension

Post by yusef88 »

thanks for your reply
my goal is keep [Arabic characters, numbers, space, -_] and remove anything else [\u0600-\u06FF|0-9|-|_|\s]
before:

Code: Select all

eng صور_22.txt
جزء اول-1 eng text.pdf
after:

Code: Select all

صور_22.txt
جزء اول-1.pdf

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: RegExp Rename modify basename and preserve extension

Post by highend »

Code: Select all

جزء اول-1 eng text.pdf
Keeping spaces would lead to:

Code: Select all

جزء اول-1  .pdf
?
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: RegExp Rename modify basename and preserve extension

Post by yusef88 »

no problem I can fix them later

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: RegExp Rename modify basename and preserve extension

Post by highend »

And your powershell snippet leads to the correct results?

Not here...

Code: Select all

eng ور_22.txt
text.txt
جزء اول-1 eng text.pdf

Code: Select all

.txt
 ور_22.txt
جزء اول1  .pdf
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: RegExp Rename modify basename and preserve extension

Post by yusef88 »

yes,PowerShell rename them correctly {windows 8.1}
2021-12-10_103837.png
if it is hard to achieve this by RegExp Rename, would you please look into my script 'in first post' and tell me if it is a correct approach to keep arabic, numbers and space
To see the attached files, you need to log into the forum.

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: RegExp Rename modify basename and preserve extension

Post by highend »

In Win 10 it produces only garbage here...

And no, your approach is not correct. Even your targeting (regexmatches) is wrong.

My recommendation, don't try that with a single regex, combine it with a loop
or just execute your powershell from a xy script...

Code: Select all

    $selected = <get "SelectedItemsNames">;
    $newSel   = "";
    $newNames = "";
    foreach($item, $selected, <crlf>, "e") {
        $base = gpc($item, "base");
        $ext  = gpc($item, "ext");

        if (regexmatches($base, ".*([\u0600-\u06FF]|[ 0-9_-]).*")) {
            $newSel   .= $item . <crlf>;
            $newName   = trim(regexreplace($base, "([^\u0600-\u06FF 0-9_-])")) . ".$ext";
            $newNames .= $newName . <crlf>;
        }
    }
    if ($newSel) {
        selectitems $newSel, , 1;
        rename "l", $newNames, "p", , 64;
    }
One of my scripts helped you out? Please donate via Paypal

yusef88
Posts: 1148
Joined: 28 Jan 2013 03:50
Location: Windows 8.1 32-bit

Re: RegExp Rename modify basename and preserve extension

Post by yusef88 »

very helpful script thank you
I've modified it a bit to avoid change current selection and prevent adding a dot to folders if folders involved in the renaming process

Code: Select all

    $keep = "^\s\u0600-\u06FF0-9-_\[\]\(\)";
    $keep = input("choose what to keep", ,"$keep");
    $selected = <get "SelectedItemsNames">;
    $newNames = "";
    foreach($item, $selected, <crlf>, "e") {
        $base = gpc($item, "base");
        $ext  = gpc($item, "ext");
        $name  = gpc($item, "name");

        if (regexmatches($base, ".*([\u0600-\u06FF]).*")) {
            if ($ext) { $ext = ".$ext"; }
            $newName   = trim(regexreplace($base, "([$keep])"));
            $newName   = RegExReplace("$newName", "\s{2,}", " ") . "$ext";
        }
        if not (regexmatches($base, ".*([\u0600-\u06FF]).*")) { $newName  = "$name"; }
            $newNames .= $newName . <crlf>;
    }
        rename "l", $newNames, "p", , 64;

Post Reply