Login  Register

Re: Filename extract and rename

Posted by Michael Entrup on Feb 04, 2016; 9:55am
URL: http://imagej.273.s1.nabble.com/Filename-extract-and-rename-tp5015526p5015536.html

Hi Richie,

if you are willing to learn a bit RegExp (regular expressions), the
following macro code can help:

```
var name = getString("Enter file name:",
"image_D2016-02-02T10-30-23-653793Z_0.JPG");
var code = "var pattern =
/\\w+_D\\d{4}-\\d{2}-\\d{2}T([\\d-_\\w]+)\\.JPG/i;\n";
code += "pattern.exec('" + name + "');\n";
code += "RegExp.$1;";
var returned = eval("script", code);
showMessage("Extracted", "The following string was extracted:\n" +
returned);
```

It uses the function ``eval()`` to run JavaScript code. There is a short
reference [1] on RegExp for JavaScript. The main concept is to use
metacharacters (you need to use 2 backslashes as the first one escapes
the second one) and quantifiers. ``\\d{4}`` stands for 4 digits.

The dot '.' is a special metacharacter that matches nearly everything.
That is why I use ``\\.``. This tells RegExp to search for the character
'.'.

``([\\d-_\\w]+)`` uses two types of groups. Everything inside the [] is
a list of possible matches. It matches to a digit, '-', '_' and a word
character. The '+' stands for one or more of these. The () will save the
content to a variable. After running ``exec()``, you can access the
variables by using ``RegExp.$1``, ``RegExp.$2`` and so on. In this case
there is only one pair of () and only ``RegExp.$1`` contains a value.

If you add () around ``\\d{4}`` and both ``\\d{2}`` you can use them to
include the date into your result. ``RegExp.$1`` will be the year,
``RegExp.$2`` the month and ``RegExp.$3`` the day. This results in
``RegExp.$4`` to be the string you search for.

var code = "var pattern =
/\\w+_D(\\d{4})-(\\d{2})-(\\d{2})T([\\d-_\\w]+)\\.JPG/i;\n";

The 'i' at the end of the pattern stands for case-insensitive matching
and allows to find 'JPG', when searching for 'jpg'.

To get the match, you have to collect the return value of ``eval()``.
This function will return the last expression of the evaluated script
(see ``runMacro()`` [2]).

Regular expression are a bit confusing, when using them for the first
time. But there is a lot of software that can use regular expressions
(e.g. lots of text editors, not including notepad . It's worth to spend
some time to learn the basics.

Best regards
Michael


[1]: http://www.w3schools.com/jsref/jsref_obj_regexp.asp
[2]: http://rsbweb.nih.gov/ij/developer/macro/functions.html#runMacro


On 03.02.2016 10:25, richieclose wrote:

> I'm looking to build out a macro which will extract all file names in a
> directory, and perform a rename on the file before saving the new file name
> out to a text file.  The first piece I can do using macro's already
> available, but I'm struggling trying to figure out the latter.
>
> A typical filename would look something like this:
> *image_D2016-02-02T10-30-23-653793Z_0.JPG* and what I would like to do is
> rename it to *T10-30-23-653793Z_0.JPG* (everything from the letter 'T'
> onwards).
>
> Any help would be much appreciated!
>
> Thanks
>
> Richie
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Filename-extract-and-rename-tp5015526.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html