Page 1 of 1
RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 04:12
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 (12.41 KiB) Viewed 1686 times
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}
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 08:09
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^^
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 08:49
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:
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 09:21
by highend
Keeping spaces would lead to:
?
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 09:26
by yusef88
no problem I can fix them later
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 09:31
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
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 09:48
by yusef88
yes,PowerShell rename them correctly {windows 8.1}

- 2021-12-10_103837.png (18.32 KiB) Viewed 1660 times
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
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 10:15
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;
}
Re: RegExp Rename modify basename and preserve extension
Posted: 10 Dec 2021 20:06
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;