Page 1 of 1

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

Posted: 29 Mar 2023 16:07
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

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

Posted: 29 Mar 2023 16:28
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...

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

Posted: 29 Mar 2023 17:57
by admin
If it's just one folder as I understand, CEA is not necessary. Look at scripting command rename k ...

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

Posted: 29 Mar 2023 18:07
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..

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

Posted: 29 Mar 2023 18:16
by admin
Quote them, this gives you a preview before committing the rename:

Code: Select all

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

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

Posted: 29 Mar 2023 18:22
by spaceman
Thank you so much!

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

Posted: 07 Apr 2023 02:41
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?

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

Posted: 07 Apr 2023 03:57
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;

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

Posted: 07 Apr 2023 11:25
by spaceman
Hi Yusef

Thank you very much.


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

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

Posted: 07 Apr 2023 13:02
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)

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

Posted: 07 Apr 2023 13:14
by spaceman
Oh ;)
Still, most people (inc. me) are deterred from RegEx.
You seem to not be.