Skip to main content
Shulipa Bernad
Inspiring
April 8, 2022
Answered

Removing characters from filenames

  • April 8, 2022
  • 4 replies
  • 1637 views

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.

 

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);
}

 

This topic has been closed for replies.
Correct answer Kukurykus
decodeURI(file.name)

4 replies

Arafin Sardar
Known Participant
April 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
Kukurykus
Legend
April 11, 2022

You mean doing everything manually?

Kukurykus
KukurykusCorrect answer
Legend
April 8, 2022
decodeURI(file.name)
Shulipa Bernad
Inspiring
April 8, 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);
}

 

 

 

Legend
April 8, 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.

Stephen Marsh
Community Expert
Community Expert
April 8, 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);
}
Shulipa Bernad
Inspiring
April 8, 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:

Stephen Marsh
Community Expert
Community Expert
April 8, 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);

 

 

Legend
April 8, 2022

Post the entire script.

Shulipa Bernad
Inspiring
April 8, 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 ".

Legend
April 8, 2022

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