I am trying to make a button to rename the parent folder from the selected file and by using embedded rename script.
@nodeselect
@firstfileonly
Rename PATTERN {filepath$|..|noterm} TO *
@script jscript
function OnGetNewName(getNewNameData) {
var item = getNewNameData.item;
var result = item.name_stem.replace(/(.*)(S\d{2}.*)(\.\[BTN\])/gi, "$2");
return result;
}
It seems I cannot manage to rename the parent folder - this button renames the selected file instead.
What am I doing wrong?
This will rename the parent folder to the filename:
@nodeselect
Rename {sourcepath} TO {file}
Changing the filename with jscript seems a bit odd. This works, but no matter what property of the getNewNameData object is picked, it will always return the complete filename:
@nodeselect
Rename {sourcepath} TO {file}
@script jscript
function OnGetNewName(getNewNameData)
{
return 'foo ' + getNewNameData.newname_stem + ' bar';
}
Aha... So thanks for pointing out, that I'd test what my script was doing.
It seems, both of your's suggestions have been working: Rename {sourcepath} TO * and Rename FROM {filepath$|..|noterm} PATTERN * TO *
The problem was, that with both of these - the parent folder was passed on to the script, not the selected file, and because I have a specialized regex in there, it was not matching the parent folder's name. So it was doing nothing.
I changed the script to simple:
function OnGetNewName(getNewNameData) {
var item = getNewNameData.item;
var result = item.name_stem + "_TEST";
return result;
}
and it renamed the parent folder...
The problem is, that the whole point was to rename from the regexed selected file
That's the point - I need to rename the folder to the selected file name, but not the exact file name - I need to regex the filename (clean it up). But I do not want the file renamed. That's why I went for this approach.
Just tried it - nope, still renames not to processed filename, but folder name.
All of these:
Rename {sourcepath} TO *
Rename {sourcepath} TO {file}
Rename FROM {filepath$|..|noterm} TO *
Rename FROM {filepath$|..|noterm} PATTERN * TO *
pass the folder not the file to the script and renames parent folder by using it own name:
from New Folder
to New Folder_TEST
This one:
Rename PATTERN {filepath$|..|noterm} TO *
correctly passes on the file, but then renames the file, not the folder...
It seems, whatever gets passed onto the script, that gets actually renamed in the end. The the goal is pass in the file, and rename the parent folder...
On the other hand, maybe there is a way to rename the parent folder using only jscript?
The Rename command works on the files it's given. If you give it the parent folder to rename, that's what it'll rename, and it's what your script will receive.
If you want to rename something that's not selected based on something that is, one way would be a script (not a rename script, just a script) that takes the selected file, works out what its parent is, then runs the Rename command with the new filename in the command line.
@nodeselect
Rename {sourcepath} TO {file}
@script jscript
function OnGetNewName(getNewNameData)
{
var item = getNewNameData.newname.replace(/(.*)\.(.*)/, '$1'); // this should remove the extension
// result = item;
result = item.replace(/(.*)(S\d{2}.*)(\.\[BTN\])/gi, '$2');
return result;
}