Rename File or Folder if the name contains certain text

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Rename File or Folder if the name contains certain text

Post by hermhart »

Can someone help me with a script that if a selected filename or folder name contains "- Copy" at the end, then have "- Copy" changed to today's current date "- (yyyy-mm-dd)". On the flip side, if it does not contain "- Copy" then just append "- (yyyy-mm-dd)" to the end of the filename/folder name (of course, before the extension if it is a file).

I found the code below here in the forum and it is very close to something that I would like. I tried plugging in the stuff that I am trying to do, but I am not having any luck.

Thank you for any help that you can provide.

Code: Select all

    foreach($item, "<get SelectedItemsPathNames |>") {
        if (exists($item) == 2) { $baseName = getpathcomponent($item, "file"); $ext = ""; } // Folder
        else { $baseName = getpathcomponent($item, "base"); $ext = "." . getpathcomponent($item, "ext"); } // File
        if (regexmatches($baseName, "-SAVE$") != "") { // Contains "-SAVE"
            $newName = regexreplace($baseName, "-SAVE$");
            copyitem $item, "<curpath>\" . $newName . $ext;
        } else { // Does not contain "-SAVE"
             renameitem($baseName . "-SAVE" . $ext, $item);
        }
    }
Windows 11 Pro; 100% scaling

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Rename File or Folder if the name contains certain text

Post by bdeshi »

Try this.

Code: Select all

  foreach ($item, "<get SelectedItemsPathNames |>") {
    if (exists($item) == 2) { $baseName = getpathcomponent($item, "file"); $ext = ""; } // Folder
    else                    { $baseName = getpathcomponent($item, "base"); $ext = "." . getpathcomponent($item, "ext"); } // File
    if (regexmatches($baseName, "\s-\sCopy$") != "") { $newName = regexreplace($baseName, "(\s-\sCopy)$", " - (<date yyyy-mm-dd>)"); }
    else                                             { $newName = regexreplace($baseName, "$", " - (<date yyyy-mm-dd>)"); }
    renameitem($newName . $ext, $item);
  }

@Don: hey, It's possible to compress the second set of if-elses into one regex pattern "(\s-\sCopy)?$" but the regex parser apparently matches both "(\s-\sCopy)" AND "$" and makes two replacements ??
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Rename File or Folder if the name contains certain text

Post by hermhart »

SammySarkar,

That works great. Thank you! That is exactly what I needed.

Do you have any suggestions on where I can do some good understandable reading for regular expressions? I just don't understand what the $ is for at the end of (\s-\sCopy)$, along with all the other character references.

Thank you again for your help in putting all that together for me.
Windows 11 Pro; 100% scaling

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

Re: Rename File or Folder if the name contains certain text

Post by highend »

Begin with e.g.: http://www.regular-expressions.info/tutorial.html

$ is the end of line anchor btw.
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 244
Joined: 13 Jan 2015 18:41

Re: Rename File or Folder if the name contains certain text

Post by hermhart »

Thank you to both of you, SammaySarkar and highend.
Windows 11 Pro; 100% scaling

Post Reply