Script or RegEx to replace multiple Search & Replace actions?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
csilvest
Posts: 3
Joined: 28 Feb 2024 22:47

Script or RegEx to replace multiple Search & Replace actions?

Post by csilvest »

Hi!

Currently I rename all files I download to have nothing more than lower-case "a-z", digits "0-9", and the hyphen "-"; I run multiple Search & Replace commands on a multi-selected group of files:

"<space>/-" (all spaces to hyphen)
"_/-" (all underscores to hyphen)
"--/-" (all double hypens to single hyphens, repeated multiple times until no replacements)

This requires a lot of clicks: 3x rclick -> Rename Special -> Search & Replace -> pick the replacement string, then 2x or "--/-" until there are no more multiple hyphens left. If I see any "oddball" characters in the filenames, I also do a "rclick -> Convert to ASCII" to convert them to my simple format.

Is this something I can make a script for? I'm pretty sure each of the Search & Replace commands can be done, but I'm confused how to operate on all the files I have multiple-selected. Examples of such scripts would be helpful.

Or is a "RegEx" operation more appropriate? I don't speak RegEx; to be honest, it scares me. :)

Cheers!

pixelsearch
Posts: 43
Joined: 21 May 2018 18:45

Re: Script or RegEx to replace multiple Search & Replace actions?

Post by pixelsearch »

csilvest, this RegEx pattern could be helpful when using RegExp Rename...

Code: Select all

( |-|_)+ > -
It means rename all spaces, hyphens, underscores (found alone or multiple) to a single hyphen
For example the following filename, containing 3 spaces between 'File' and 'ABC' :

Code: Select all

File   ABC_123-456---___789___---012.txt
will be Regexp Renamed, in a single pass, to :

Code: Select all

File-ABC-123-456-789-012.txt

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

Re: Script or RegEx to replace multiple Search & Replace actions?

Post by highend »

Code: Select all

    $items = <get SelectedItemsNames>;
    end (!$items), "No item(s) selected, aborted!";

    $items = recase($items);
    $items = regexreplace($items, "( |-|_)+", "-");
    $new   = "";
    foreach($item, $items, <crlf>, "e") {
        $new .= regexreplace(gpc($item, "base"), "[^a-z0-9-]+", , 1) . "." . gpc($item, "ext") . <crlf>;
    }
    $new = trim($new, <crlf>, "R");
    if ($new != $items) {
        rename "l", $new, "p", , 64;
    }

Code: Select all

@why-do__we     have this ^here..MP4
This is üetting me mad #or not!.txT

=>

this-is-etting-me-mad-or-not.txt
why-do-we-have-this-here.mp4
One of my scripts helped you out? Please donate via Paypal

csilvest
Posts: 3
Joined: 28 Feb 2024 22:47

Re: Script or RegEx to replace multiple Search & Replace actions?

Post by csilvest »

pixelsearch wrote: 09 May 2024 16:32 csilvest, this RegEx pattern could be helpful when using RegExp Rename...

Code: Select all

( |-|_)+ > -
It means rename all spaces, hyphens, underscores (found alone or multiple) to a single hyphen
For example the following filename, containing 3 spaces between 'File' and 'ABC' :

Code: Select all

File   ABC_123-456---___789___---012.txt
will be Regexp Renamed, in a single pass, to :

Code: Select all

File-ABC-123-456-789-012.txt
Thank you! For both the regex expression and the explanation. From them, I was able to remove commas as well:

Code: Select all

( |-|_|,)+ > -
But then I realized this is backwards; I only want a few specific characters ("a-z", digits "0-9", and the hyphen "-"), which I think regex is:

Code: Select all

([a-z]|[0-9]|-)+
So I need to add a "^" to take the NOT of this, but no matter where I put that "^", doesn't work (using regexr.com to test & learn). No amount of carefully reversing the ORs to ANDs or otherwise adjusting this regex works. Obviously, I'm just guessing. :) But learning, too! I think my best guess is:

Code: Select all

(^[a-z]^[0-9]^-)+ > -
Thanks a bunch!

jupe
Posts: 3292
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Script or RegEx to replace multiple Search & Replace actions?

Post by jupe »

Something like this: [^a-z0-9-]+ > - but you should check out XY function "Keep Particular Characters" for this task.

Post Reply