# Rename scripts in JScript, VBScript and PerlScript

**URL:** <https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480>\
**Category:** Rename Presets\
**Tags:** script, vbscript, jscript, perl\
**Created:** [February 10, 2008, 1:55am UTC](https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480 "2008-02-10T01:55:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [February 10, 2008, 1:55am UTC](https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480/1 "2008-02-10T01:55:44Z")

</div>

_Updated Aug 29, 2017, for Directory Opus 12._

You do not have to use scripting to batch-rename files in Opus. You can use simple wildcards, find & replace, regular expressions, and so on. **Rename scripts are just an option for when you need more power.**

When using a rename script, Opus will call a function for each file or folder. The function is passed information about each thing being renamed, such as its old name and proposed new name (including any other effects of the Rename dialog's non-script settings). The script can then approve, reject or change the proposed new name based on whatever logic you wish to use.

New rename scripts should implement a function called `OnGetNewName` which will be passed a `getNewNameData` object with various information about the item being renamed, including access to its metadata. (The data this object provides may be expanded over time, without breaking existing scripts.)

In Opus 12, if you open the rename script editor and select JScript or VBScript, you'll get a default script which walks you through the basic details. (You can also save your own default script to use as a starting point, to save time in the future.)

 ![Default JScript Rename Script](https://resource.dopus.com/uploads/default/original/3X/0/0/0021a1620dfb61404152c8d9f37cfcf3437bb7f6.png)

Important details are documented in these manual pages:

- [Rename Scripts](https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Rename_Scripts1.htm)
- [OnGetNewName](https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/OnGetNewName.htm)
- [GetNewNameData](https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/GetNewNameData.htm)

Instead of `OnGetNewName`, older rename scripts may use the legacy `Rename_GetNewName` or `Rename_GetNewName2` functions, which were passed details via multiple parameters. There is no reason to use those in new scripts, and they are only worth knowing about in case you find an old script which uses them.

(In a couple of old JScript posts, you may see `Rename::GetNewName` or `Rename::GetNewName2` used. The `::` turn into `_` behind the scenes and they run the same legacy functions as in the previous paragraph.)

Below are two simple rename script examples in JScript and VBScript.

The examples do the same same thing: Add a ` TEST` suffix to each filename, before any file extension. The examples are not supposed to be useful by themselves; they exist purely to demonstrate the appropriate syntax for rename scripts in various languages. More complex examples can be found in the forum.

They also show how you can use `DOpus.Output` to print debugging information to the panel below the script editor.

After making changes to a script, click the **Refresh Preview** button above the editor to see the results. Also note that if you modify parameters in the main part of the Rename dialog, it can cause your script to be re-run to update the preview. Keep that in mind if your script has any external side-effects (such as modifying files on its own).

* * *

**JScript:**

JScript support is built into Windows.

Set the _Script Type_ field to `JScript`.

```jscript
function OnGetNewName(getNewNameData)
{
	var item = getNewNameData.item;
	var result = item.name_stem + " TEST" + item.ext;

	DOpus.Output("Name = " + item.name);
	DOpus.Output("Name Stem = " + item.name_stem);
	DOpus.Output("Extension = " + item.ext);
	DOpus.Output("Result = " + result);
	DOpus.Output("---------");

	return result;
}

```

 ![200dpi_wc10277_dopus](https://resource.dopus.com/uploads/default/original/3X/7/8/785f9cec78471fb8ea41063c1a2be439f0c64bbb.png)

* * *

**VBScript:**

VBScript support is built into Windows.

Set the _Script Type_ field to `VBScript`.

```vbscript
Option Explicit
Function OnGetNewName(ByRef getNewNameData)
	Dim item, result
	Set item = getNewNameData.item
	result = item.name_stem & " TEST" & item.ext

	DOpus.Output "Name = " & item.name
	DOpus.Output "Name Stem = " & item.name_stem
	DOpus.Output "Extension = " & item.ext
	DOpus.Output "Result = " & result
	DOpus.Output "---------"

	OnGetNewName = result
End Function

```

 ![200dpi_wc10278_dopus](https://resource.dopus.com/uploads/default/original/3X/2/9/29e3f11c2b9ca3d4c16330d983d759c7f9eca05f.png)

* * *

**PerlScript:**

To use PerlScript you must install [ActivePerl](http://www.activestate.com/Products/activeperl/), or an equivalent ActiveScripting version of Perl.

If you plan to make rename scripts to share on the forums, please consider using JScript or VBScript instead of PerlScript, so that people can use the scripts they download out-of-the-box.

You'll also generally find it easier to use JScript or VBScript as there are more examples on the forum, more people who can help with them here, and you're less likely to run into unexpected language-level issues due to Opus's scripting support being mostly designed and tested around the built-in languages.

Credit for this example goes to the combined efforts of @MrC and @myarmor.

Note that this example prefixes the filename with `Test_`, slightly different to the other examples.

Set the _Script Type_ field to `perlscript` (if it is not in the drop-down already, you can type it manually).

```perl
use v5.14;
use strict;
use warnings;
use warnings qw(FATAL utf8);
use utf8;
use Win32::OLE qw(CP_UTF8 CP_ACP);
use Win32::OLE::Variant qw(Variant VT_BSTR);
Win32::OLE->Option(Warn => 3, CP => CP_UTF8);
sub OnGetNewName{
  my ($GetNewNameData)=@_;
  my $ret="Test_".$GetNewNameData->item->name;
  return Variant(VT_BSTR, $ret);  
}

```

(Most of the perlscript example is boilerplate code to make perlscript use Unicode. In contrast, Unicode support is automatic with JScript and VBScript.)

From @myarmor:

> Note that Opus rename editor uppercases the "s" in "sub", so you'll have to change that by doubling the "s" then use backspace on the first. Now when the initial "s" is lowercase you can save it as usual, and it will run correctly even though the editor displays it as "Sub" whenever the saved script is selected.

* * *

**Other Languages:**

In theory, if a language has an ActiveScripting port which is installed on your system (and is compiled for 64-bit, if using 64-bit Windows), then you can use it to write scripts in Opus, including rename scripts.

In practice, it can be an uphill battle, as many scripting languages have wildly different rules about argument passing (references vs values, etc.), reserved names and other keywords, properties vs methods, and other issues you probably don't want to deal with. If you know one language already, it is usually easy to learn another (the differences tend to be quite minor), and we recommend using JScript or VBScript.

If you don't know JScript or VBScript, learning JScript is probably the most useful, as it is closely related to JavaScript (it is essentially a slightly older version of JavaScript), and you'll find JavaScript is useful to know for almost everything that involved the web. But it doesn't matter a great deal, and if you learn one it is easy to then learn the other.

* * *

**Saving your Rename Script for use in the Rename dialog**

You would normally save your script into a Rename Preset, which saves the setup of the rename dialog into a named preset. The list of presets is on the left of the Rename dialog in Opus 12, and on the right in earlier versions. The buttons above the list let you load and save presets, among other things.

* * *

**Using your Rename Script outside the Rename Dialog**

If you wish to run your script from a toolbar button, instead of from within the Rename window, there are several ways to do it. Which you choose depends on where you want the script to be defined.

Although it is not required, it is usually nice to use `@nodeselect` with buttons that rename files, since you often want to keep them selected to do other things with them afterwards. The examples below include this.

1. **Reference a Rename Preset from a button:**

2. **Use the Rename Preset menu:**

3. **Everything in a Button:**

4. **Not using a rename script at all:**

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [August 31, 2017, 6:18pm UTC](https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480/19 "2017-08-31T18:18:24Z")

</div>

Thread has been updated and cleaned up for Opus 12.

---

<div class="post-metadata">

**Author:** ![doptrick](https://resource.dopus.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@doptrick](https://resource.dopus.com/u/doptrick)\
**Post date:** [September 21, 2018, 5:00pm UTC](https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480/20 "2018-09-21T17:00:46Z")

</div>

Thanks @Leo, this post was most helpful. I was originally trying to create a rename button script but forgot the built-in Rename option has scripting, which is what I needed. My use-case for this was wanting to read JSON files (Qualtrics survey template files) and parse a JSON parameter to use as the rename value. Here is the javascript code I used:

```auto
function OnGetNewName(getNewNameData)
{
	var item = getNewNameData.item;
	// open the survey template file and extract the JSON data
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var fh = fso.OpenTextFile(getNewNameData.item.realpath, 1);	// open the file for reading
	var fText = fh.ReadAll();
	var objSurvey = JSON.parse(fText);
	fh.Close();
	// rename the template file to the survey ID defined in the data
	var result = objSurvey.SurveyEntry.SurveyID+".json";
	// testing output
	/*
	DOpus.Output("Name = " + item.name);
	DOpus.Output("Name Stem = " + item.name_stem);
	DOpus.Output("Extension = " + item.ext);
	DOpus.Output("Result = " + result);
	DOpus.Output("Path = " + getNewNameData.item.realpath);
	DOpus.Output("---------");
	*/
	return result;
}
```

DOpus is a lifesaver!
