# RecentToCollection (Create a collection from Recent folders)

**URL:** https://resource.dopus.com/t/recenttocollection-create-a-collection-from-recent-folders/48571
**Category:** Buttons/Scripts
**Tags:** script, commands, jscript
**Created:** [February 3, 2024, 2:41pm UTC](https://resource.dopus.com/t/recenttocollection-create-a-collection-from-recent-folders/48571 "2024-02-03T14:41:36Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [February 3, 2024, 2:41pm UTC](https://resource.dopus.com/t/recenttocollection-create-a-collection-from-recent-folders/48571/1 "2024-02-03T14:41:37Z")

</div>

**RecentToCollection** turns the folders from the _Recent_ node in the folder tree into a collection and opens it in the source.

#### How to set up and use

1️⃣ Save [CommandRecentToCollection.js.txt](https://resource.dopus.com/uploads/short-url/8Z0xWaZpP0V7hhr0jNldiVCUw9x.txt) to   ↓

```plaintext
%appdata%\GPSoftware\Directory Opus\Script AddIns

```

2️⃣ Add the new command to a button, hotkey, context menu, or the startup configuration like any built-in command, or run it from the FAYT Command field.

#### Things you might enjoy reading

Inspired by [Recent Locations for new lister](https://resource.dopus.com/t/recent-locations-for-new-lister/48564)

[How to use buttons and scripts from this forum](https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546)

#### The script's inner workings

```javascript
function OnInit(initData) {
    initData.name = 'RecentToCollection';
    initData.version = '2024-02-03';
    initData.url = 'https://resource.dopus.com/t/recenttocollection-create-a-collection-from-recent-folders/48571';
    initData.desc = '';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'RecentToCollection';
    cmd.method = 'OnRecentToCollection';
    cmd.desc = '';
    cmd.label = '';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnRecentToCollection(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var stt = DOpus.Create().StringTools();
    var fsu = DOpus.FSUtil();

    cmd.deselect = false;

    var recentItem = fsu.GetItem(fsu.Resolve('/dopuslocaldata\\State Data\\recent.osd'));
    if (!fsu.Exists(recentItem)) return;

    var newColl = 'coll://Recent';

    var recentArr = stt.Decode(recentItem.Open().Read(), 'utf8').split('\r\n');
    var re = /.*<pathstring>(.*)<\/pathstring>.*/;

    cmd.ClearFiles();
    for (var i = 0; i < recentArr.length; i++) {
        var tmp = recentArr[i].match(re);
        if (!tmp) continue;
        if (tmp.length != 2) continue;
        var path = tmp[1];
        if (!fsu.Exists(path)) continue;
        if (path == newColl) continue;
        cmd.AddFile(path);
    }

    cmd.RunCommand('Delete FILE="' + newColl + '" QUIET');
    cmd.RunCommand('CreateFolder NAME="' + newColl + '"');
    cmd.SetDest(newColl);
    cmd.RunCommand('Copy COPYTOCOLL=member');
    cmd.RunCommand('Go PATH="' + newColl + '"');
}

```
