Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Removing characters from filenames

Engaged ,
Apr 08, 2022 Apr 08, 2022

Hello friends, how are you?
I'm having trouble modifying a script whose purpose is to remove all special characters, spaces and letters leaving only the digits that represent the numerical order in the name of all files in a folder, but I found an error when there are Parentheses [ ( ) ] "(1).jpg ",  =  201.jpg .
I don't know how to remove the unwanted initial two digits, could anyone help me out there? Thanks.

Captura de tela 2022-04-08 084543.jpg

 

var folder = Folder.selectDialog("Selecione a pasta de destino")
var files = folder.getFiles();
var file, extension;
for (var i = 0; i < files.length; i++){
    file = files[i];
    extension = file.name.slice(file.name.length-4, file.name.length);
    file.rename(file.name.replace(/[^\d]+/g,"")+extension);
}

 

TOPICS
Actions and scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 08, 2022 Apr 08, 2022
decodeURI(file.name)
Translate
Adobe
LEGEND ,
Apr 08, 2022 Apr 08, 2022

Post the entire script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 08, 2022 Apr 08, 2022

Hi @Lumigraphics , the script was posted complete above, no more and no less, do you receive any error messages? Just select a folder with some files that have Parentheses [( ) ] "(1).jpg ".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2022 Apr 08, 2022

That's not the whole script, your regular expression just replaces one or more numerical digits.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2022 Apr 08, 2022

One method:

 

var folder = Folder.selectDialog("Selecione a pasta de destino")
var files = folder.getFiles();
var file, extension;
for (var i = 0; i < files.length; i++){
    file = files[i];
    extension = file.name.replace(/(?:.+)(\.[^\.]+$)/, '$1');
    file.rename(file.name.replace(/(?:.+\()(\d+)(?:\).+)/g, "$1") + extension);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 08, 2022 Apr 08, 2022

Hi @Stephen Marsh  the main objective is to eliminate all characters, letters and spaces, leaving only the digits of any file name, even if it has parentheses or not. Does not work for all names:

Captura de tela 2022-04-08 103612.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2022 Apr 08, 2022

Ah, sorry, I made it explicit to your initial example, not robust and general purpose. Now that I can see your new post, I understand what you are after now.

 

The reason that I removed the slice in the extension and used a regex was that not all filename extensions are a period + 3 characters (.ai files for example).

 

I recently discovered .displayName which also works in this case:

 

 

file.rename(file.displayName.replace(/[^\d]+/g,"")+extension);

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 08, 2022 Apr 08, 2022

All of you are geniuses.
@Stephen Marsh, hanks for sharing your knowledge.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2022 Apr 08, 2022
decodeURI(file.name)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 08, 2022 Apr 08, 2022

@Kukurykus your solution worked perfectly. Thanks again for your help.
Fully functional run script:

var folder = Folder.selectDialog("Selecione a pasta de destino")
var files = folder.getFiles();
var file, extension;
for (var i = 0; i < files.length; i++){
    file = files[i];
    extension = file.name.slice(file.name.length-4, file.name.length);
    file.rename(decodeURI(file.name).replace(/[^\d]+/g,"")+extension);
}

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2022 Apr 08, 2022

That's still not a good regex.

^ from the beginning

[\d] replace a numerical digit

+ one or more instances

/g global find

If you have global find, it makes no sense to specify from the beginning. Which do you want?

Replace one or more should be [\d+] not [\d]+ although I'm not sure if the regex parser will accept both.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2022 Apr 08, 2022

The second kind of use for ^ lets avoid characters put inside of Regex square brackets.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 08, 2022 Apr 08, 2022
@Lumigraphics, i confess to you that I don't know how to program a script, I just have a little idea and I modify some to meet my need, regex is something that I have a lot of difficulty understanding. Thanks for your precious tips.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2022 Apr 09, 2022

@Lumigraphics as mentioned by @Kukurykus â€“ [^\d]+ is a valid regex and likely the best option for the use case. I believe that it may be known as exception or negation. The + can't be included inside the [^] as then it would be excluding that literal character as it is no longer behaving as a meta-character in this situation, the context of the character has changed. In this particular use case, either [^\d]+ or [^\d] should be equivalent in the end result of removing any character that is not a digit.

 

EDIT: An equivalent alternative should be:

 

.replace(/\D/g,"")

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2022 Apr 11, 2022

I find regex terribly confusing and clunky LOL. I can slap something together but have to test it to make sure it works properly. I'm still not seeing how the OP accomplishes his original task.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2022 Apr 11, 2022

I like regex, even if I don't always understand it or come up with a good solution, and yes, it always should be tested (regex101, regexr, or other websites or tools).

 

The OP just wants to delete everything that is not a \d digit character. Nothing more, nothing less.

 

So the following are all valid and equivalent regex searches, to be replaced with nothing:

 

[^\d]

 

[^\d]+

 

[^0-9]

 

[^0-9]+

 

\D

 

\D+

 

What initially caught me out was the leading 20 characters that "magically appeared from nowhere" in some filename strings... until it became obvious that these were leftovers from URI %20 characters for spaces.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2022 Apr 11, 2022
LATEST

Oooohhhh... I though he was trying to number them in some specific order.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 11, 2022 Apr 11, 2022

It is better to download this file and save it in a folder.  And then select it and click the right button on the mouse. Then you will get an option to rename it. From where you can change the file name.

Professional Product Photo Editor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2022 Apr 11, 2022

You mean doing everything manually?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines