How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
spaceman
Posts: 9
Joined: 06 Aug 2021 15:05

How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by spaceman »

Hi all


I have a folder that contains many files,
and I want to clean the filenames of these files from problematic chars,
and only leave chars that I will define in a White List of chars,
for example:
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()-_."

All other chars will be deleted from the filenames..


Can XYplorer can do this automatically on a folder, If I provide the White List of chars?


Thank you

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by highend »

With a script that would loop over all files in that folder, check the names and remove the chars and that one bound to a CEA: Changing locations

It would at least be triggered once you switch to that folder...
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by admin »

If it's just one folder as I understand, CEA is not necessary. Look at scripting command rename k ...

spaceman
Posts: 9
Joined: 06 Aug 2021 15:05

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by spaceman »

Thank you very much regarding rename k.

What is CEA?
The documentation does not mention this term..


The folder has some sub-folders,
so iterating it recursivly would be nice,
but even If not, I can manually execute it on each of the folders myself,
since the total number of folders is not big.


BTW,
when I supply the list of Allowed Chars to rename k,
how do I prevent some chars from being interpreted as a part of the command?
(e.g. ',' for example)
I need the command to understand all the chars as the chars in the Allow List,
and that no char will "break" or ruin the syntax of the line..

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by admin »

Quote them, this gives you a preview before committing the rename:

Code: Select all

rename k, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()-_.", p;


spaceman
Posts: 9
Joined: 06 Aug 2021 15:05

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by spaceman »

Can the rename command perform 2 modes on 1 execution?

For example:
Search and Replace:

Code: Select all

rename s, "[]{};,!>_"
And:

Code: Select all

rename k, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()-_.", p;
I can do it with 2 commands,
but doing it with 2 commands has 2 problems:
The first (the small problem) is that it will go over all files twice, in order to rename them,
and the second (the biggerl problem) is that I can't have the preview on the total change that will happen..
I can only have a preview twice, or a preview on the last 1 (mode k), butout the change in mode s.

So does the syntax allow tosomehow combine more than 1 mode, into 1 command?

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

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by yusef88 »

I hope this will help you.

Code: Select all

 $names = "";
 foreach($item, <selitems <crlf>>, <crlf>, "e") {
  $str = gpc($item, "file");
  $str = regexreplace("$str", "[\[|\]||{|}|;|,\!]", "_");
  $str = regexreplace("$str", "[^a-zA-Z0-9\(\)-_\.]", "");
  $names .= $str . <crlf>; }
  rename "l", $names, p, , 64;
Last edited by yusef88 on 07 Apr 2023 13:02, edited 1 time in total.

spaceman
Posts: 9
Joined: 06 Aug 2021 15:05

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by spaceman »

Hi Yusef

Thank you very much.


It's nice that you get along well with RegEx,
most people are detered by them.. :)

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

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by yusef88 »

actually, I'm not experienced in regex. the script belongs to highend. viewtopic.php?p=193995#p193995
I post it as example how to do rename in one go (regex maybe inaccurate)

spaceman
Posts: 9
Joined: 06 Aug 2021 15:05

Re: How to Clean the Filenames of Files in a Folder, According to a white List of Chars, Automatically?

Post by spaceman »

Oh ;)
Still, most people (inc. me) are deterred from RegEx.
You seem to not be.

Post Reply