Copy link to clipboard
Copied
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);
}
decodeURI(file.name)
Copy link to clipboard
Copied
Post the entire script.
Copy link to clipboard
Copied
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 ".
Copy link to clipboard
Copied
That's not the whole script, your regular expression just replaces one or more numerical digits.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Hi @Stephen_A_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:
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
All of you are geniuses.
@Stephen_A_Marsh, hanks for sharing your knowledge.
Copy link to clipboard
Copied
decodeURI(file.name)
Copy link to clipboard
Copied
@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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
The second kind of use for ^ lets avoid characters put inside of Regex square brackets.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@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,"")
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Oooohhhh... I though he was trying to number them in some specific order.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You mean doing everything manually?