GP SoftwareTwitter
Opus FAQsManualCommandsObjects

[Solved] Rename parent folder button using embedded rename script

scripting
rename

#1

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?

Thanks in advance for any help.


getNewNameData extensions when renaming a folder
#2

Use FROM rather than PATTERN.


#3

Using FROM does not work at all - nothing gets renamed :confused:


#4

I'd try Rename {sourcepath} TO


#5

You may need both, e.g. Rename FROM {filepath$|..|noterm} PATTERN * TO *.


#6

Nope :slight_smile:

Both: Rename {sourcepath} TO *
and Rename FROM {filepath$|..|noterm} PATTERN * TO *
doesn't work...

Nothing gets renamed in both cases.


#7

Have you checked what your script is doing? The command itself won't do anything other than invoke the script.


#8

This works for me in a regular button:

@firstfileonly
Rename {filepath$|..|noterm} TO {file$|noext}

#9

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

#10

@Jon and @lxp

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 :slight_smile:

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.


#11

Replace the last * with {file}.


#12

@lxp

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?


#13

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.


#14

Give this a try:

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

#15

@lxp

Thank You

That does exactly what I needed :tada:

Now... I need to figure out how you did it...