#1 : 10/03-23 20:41 Baki UÄŸur
Posts: 14
|
Hi everyone.
I have hundreds of files named in a certain pattern. The general structure goes like this (Xxx - @ Xxx). I want all letters to be uppercase up to the first ( -) dash. How do I do this? |
#2 : 10/03-23 22:33 Baki UÄŸur
Posts: 14
|
Reply to #1:
ChatGPT created me the following code as Script. However, it doesn't work. What is the reason? function execute(item) { var filename = item.name; var parts = filename.split("-"); var firstPart = parts[0].toUpperCase(); var secondPart = parts.slice(1).join("-"); var newFilename = firstPart + "-" + secondPart; return newFilename; } |
#3 : 10/03-23 23:01 David Lee
Posts: 1125
|
Reply to #2:
It works perfectly - if you actually execute the function! What you have done is to define a function: "execute()" that takes an item object as an argument. You would normally place the function in the Pre batch script so it is only defined once before the batch is executed. Then you would execute it in the main script as: return execute(item); However, the main script is itself a function - if you look at the script window you will see that the line "function (index, item) {" is pre-defined at the top with "}" at the bottom. You can just copy your executable code into the script window as the body of the pre-defined function, thus: var filename = item.name; var parts = filename.split("-"); var firstPart = parts[0].toUpperCase(); var secondPart = parts.slice(1).join("-"); var newFilename = firstPart + "-" + secondPart; return newFilename; However, in this case you don't need to use a script at all. Just use a "Replace" method with a regular expression: Text to be replaced: (^.*-) Replace with: \U1 Use regular expressions The regular expression will match all characters (".*") starting from the beginning of the filename ("^") as far as the first instance of "-". Placing the expression within parentheses means that the result will be saved in a numbered variable, "\1", which can be used in the replacement string and adding "U" will convert the contents of the variable to upper case. Whilst it's totally unnecessary in this case, it's well worth remembering how to create a JavaScript function as it could be very handy if you ever have a complicated renaming problem. |
#4 : 11/03-23 22:16 Baki UÄŸur
Posts: 14
|
Reply to #3:
First of all, thank you for this explanatory information. Actually, I asked ChatGPT how to do it without code first, but it couldn't provide any useful code or explanation. This (^.*-) , \U1 worked but I guess it has a bug. If there are 2 "Dash (-)" in the filenames to be changed, it changes them all to CAPS. I want it to be UPPERCASE until the first "Dash (-)" section. How do we fix this. |
#5 : 11/03-23 23:33 David Lee
Posts: 1125
|
Reply to #4:
Sorry - I forgot that by default the "*" quantifier is "greedy" - ie it will always match as many characters as possible so that it will terminate at the latest instance of "-". You can set it to "lazy" mode by adding the modifier "?", when the regex will match the minimum number of characters and so will terminate at the first instance of "-". ie Text to be replaced: (^.*?-) Alternatively a better regex would be: (^[^-]*-) There is no confusion using this regex and the match will always terminate with the first instance of "-". Here "[^-]" indicates any character except "-" and in this context "^" is interpreted as "NOT", rather than "start of string". |
#6 : 12/03-23 22:13 Baki UÄŸur
Posts: 14
|
Reply to #5:
Thanks, it worked. You are the king man. :) |