# Select most recently modified file in the current directory?

**URL:** <https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974>\
**Category:** Help & Support\
**Created:** [February 5, 2013, 3:40pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974 "2013-02-05T15:40:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 5, 2013, 3:40pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/1 "2013-02-05T15:40:05Z")

</div>

I've often wished that I can press a shortcut to select the most recently modified file in the current directory.

This would particurlarly useful if you navigate to your download folder (containing many files) and you just have to press a single shortcut to navigate and select the file you downloaded a few seconds ago.

Since the select command doesn't provide an appropriate argument I'm wondering if there is any other solution to achieve that?

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 5, 2013, 3:57pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/2 "2013-02-05T15:57:49Z")

</div>

BTW, my workaround I have used so far looks like that:

```auto
Set SORTORDER=Files
Set SORTBY=modified 
Select FIRST 
Set SORTORDER=Folders
Set SORTBY=name 
```

But it feels really inconvenient because everything is re-layouted multiple times and it doesn't keep the original sort state, instead after performing this command the file display is always in name/folders mode (which obviously may not be your initial configuration). Thus I would really be happy to have a dedicated command (or argument) for this ...

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 5, 2013, 4:10pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/3 "2013-02-05T16:10:19Z")

</div>

Here is a little improvement to make the behaviour more stable against reversed sort direction:

```auto
Set SORTORDER=Files
Set SORTBY=modified 
Set SORTREVERSE=Off
Select FIRST 
Set SORTORDER=Folders
Set SORTBY=name 
```

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 5, 2013, 4:41pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/4 "2013-02-05T16:41:05Z")

</div>

Such a native command should support multiple arguments for selection like select RECENT=filesfirst,created,modified. This means select the file (if no files than folder) that was recently created or modified whatever was later.

---

<div class="post-metadata">

**Author:** ![D0xer](https://resource.dopus.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@D0xer](https://resource.dopus.com/u/D0xer)\
**Post date:** [February 7, 2013, 6:24am UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/5 "2013-02-07T06:24:49Z")

</div>

+1

I like that idea very much.

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 8, 2013, 7:06am UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/6 "2013-02-08T07:06:57Z")

</div>

I still have the problem, that I can not create a _single_ script that take the created as well as the modified time into account. This means I want to select the most recently modified _or_ created file (whatever was later). This is particurlarly needed to catch files coming from extracted archives (which often restores the modification time). After extracting an archive my script mostly fails.

The main idea behind my script (that is given above) can't take both times into account. That was the reason why I asked for a new command.

Now I'm wondering if it is possible to write a small VBScript for this puprpose or are VBScripts only applicable for renaming?

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 11, 2013, 12:59pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/7 "2013-02-11T12:59:54Z")

</div>

Leo, do you have any nice idea how to select the most recent file in the current directory considering the modified _and_ the creation time (whatever was later)?

---

<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 11, 2013, 1:03pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/8 "2013-02-11T13:03:28Z")

</div>

No.

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [February 14, 2013, 5:00pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/9 "2013-02-14T17:00:39Z")

</div>

I have read the JScript documentation and was able to write a small script that finds out the most recently modified or created file.

Unfortunately I'm not sure how I can use this script in a command!?

To be more precise I'm wondering if I can embed my script directly into a DOpus command (e.g. using the @script modifier)!? I don't want to have the script flying around in my filesystem, instead I want to keep things together (e.g. the script should work in the exported portable version as well). Thus how can I configure a single command that executes my script code (given below) without the need to save the script into an actual file!?

```auto
var args = WScript.arguments;
if (args.length > 0) {
    var ws = WScript.CreateObject("WScript.Shell");
    var fs = new ActiveXObject('Scripting.FileSystemObject');
    var folder = fs.GetFolder(args(0));
    var youngestFile, youngestFileDate;
    for (files = new Enumerator(folder.files); !files.atEnd(); files.moveNext()) {
        var file = files.item();
        var fileDate = Math.max(file.DateCreated, file.DateLastModified);
        if (fs.FileExists(file) && youngestFile == null || fileDate > youngestFileDate) {
            youngestFile = file;
            youngestFileDate = fileDate;
        }
    }
    if (youngestFile != null) {
        ws.Run("dopusrt /acmd Select " + youngestFile + " DESELECTNOMATCH MAKEVISIBLE");
    }
} else {
    WScript.Echo("argument missing");
}
```

---

<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 14, 2013, 5:11pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/10 "2013-02-14T17:11:00Z")

</div>

@script is only for rename scripts, but you can still take advantage of it, like this:

> [@\[OBSOLETE\] "Abusing" Rename Scripts to do other things with file info](https://resource.dopus.com/t/abusing-rename-scripts-to-do-other-things-with-file-info/5969/1):
>
> Directory Opus 11 and above have proper scripting support for normal buttons, hotkeys, etc. There is no longer any reason to abuse the Rename command in order to run scripts. You can simply run scripts directly, which is much better. This post was …

---

<div class="post-metadata">

**Author:** ![trevorm](https://resource.dopus.com/user_avatar/resource.dopus.com/trevorm/32/50714_2.png) [@trevorm](https://resource.dopus.com/u/trevorm)\
**Post date:** [July 2, 2013, 2:59pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/11 "2013-07-02T14:59:32Z")

</div>

+1 for this request.

---

<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:** [July 3, 2013, 10:04am UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/12 "2013-07-03T10:04:55Z")

</div>

The next update will have new Select DATE and Select SIZE commands which can do this, and a few other things.

---

<div class="post-metadata">

**Author:** ![AKA-Mythos](https://resource.dopus.com/user_avatar/resource.dopus.com/aka-mythos/32/26158_2.png) [@AKA-Mythos](https://resource.dopus.com/u/AKA-Mythos)\
**Post date:** [July 3, 2013, 10:41am UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/13 "2013-07-03T10:41:57Z")

</div>

Glad to hear that. 👍

Will the Select DATE command consider the modified _and_ the creation time (whatever was later)?

---

<div class="post-metadata">

**Author:** ![trevorm](https://resource.dopus.com/user_avatar/resource.dopus.com/trevorm/32/50714_2.png) [@trevorm](https://resource.dopus.com/u/trevorm)\
**Post date:** [July 3, 2013, 12:15pm UTC](https://resource.dopus.com/t/select-most-recently-modified-file-in-the-current-directory/14974/14 "2013-07-03T12:15:26Z")

</div>

Sweet. Thanks for the update, Leo.
