Operating System - OpenVMS
Showing results for 
Search instead for 
Did you mean: 

How to find latest created file in VMS ?

Sk Noorul Hassan
Regular Advisor

How to find latest created file in VMS ?


There are numbers of files created in a directory (inside sub directories also) every day. How to find the last file created in this directory as I want to see the time, the file has been created. Please suggest the command to get it.
Willem Grooters
Honored Contributor

Re: How to find latest created file in VMS ?

a /SORT= qualifier on DIR is a long wished addition to the DIR command....

As long as it doen't exits, yopu'll have to do it by a command procedure. A simple SORT on date on a DIR output file won't do the job since all dates will be displayed in VMS format (dd-MMM-yyyy hh:m:ss.ccc) which is not a format easy to sort...

A quick hack (not tried)

Either prepare DIR/DATE/OUT= - add /WIDTH=, /COLUMNS= /NOHEADER and /NOFOOTER options to format the file - open that file and read one by one, reformat the date to COMPARISON format 0(yyyymmddhhmmssccc) using F$VCTIME, and write that in front of the filename into an output file. Ir scan the directory tree useing F$SEARCH, extract creation date using F$FILE_ATTRIBUTES, convert the result to the comparin format and do the write. Then sort the output file.

Willem Grooters
OpenVMS Developer & System Manager
Willem Grooters
Honored Contributor

Re: How to find latest created file in VMS ?

Perhaps this might be what you need - just thought about it.


Willem Grooters
OpenVMS Developer & System Manager
Karl Rohwedder
Honored Contributor

Re: How to find latest created file in VMS ?

Kris Clippeleyr provided a utility called QDSB (Directory sorted by), which produces sorted directory listings.
Check http://www.quadratrix.be/products.html, if it suits your needs.

regards Kalle
Hein van den Heuvel
Honored Contributor

Re: How to find latest created file in VMS ?

Note, DIRECTORY does NOT have a sort by date (nor size) because the OpenVMS directory entries only have a name + fileID. This is unlike Unix where the inodes have file characteristics such as data and protection.

So for Unix it is cheap, but for OpenVMS it was considered too expensive (1 IO per file) to give an easy sort by. Of course the end result was that many folks spend energy sorting poorly where directory could have done it better. Expensive, but better.


You can tell sort a magic collating sequence to get the (english) months right.
See old topic (1996) in c.o.v


Or more recently here:


And finally, here is a perl 'one liner':

$ perl -e "while (<*.tmp;*>) { $f{$_}=-M } foreach $x (sort {$f{$b}<=>$f{$a}} keys %f){ print qq($x\n)}"

That was sorted on modification date, new to old. For new to old switch the $a and $b around.


Hein van den Heuvel
Honored Contributor

Re: How to find latest created file in VMS ?

I did not read the question careful enough.
If you just want to see the last file, then doign the x=F$SEARCH + F$FILE(x,"CDT") preferable over sort.

For example:

$ file = F$SEARCH("[...]*.tmp;*")
$ last = ""
$ this = F$CVTIME(F$FILE(file,"CDT"))
$ IF this.GTS.last
$ most_recent = file
$ last = this
$ file = f$search("[...]*.bas;*")
$ IF file .NES. "" THEN GOTO loop
$ WRITE sys$output most_recent, " ",F$FILE(most_recent,"CDT")

In perl it could look something like:

perl -e "$M=9999; while (<[...]*.bas;*>) { $m = -M; if ($m<$M) {$M=$m; $f=$_}} print qq($f\n)"


Robert Gezelter
Honored Contributor

Re: How to find latest created file in VMS ?


There is a way to do this without a lot of fuss. It does involve the use of the DIRECTORY and SORT commands, and it does require a short preparatory step.

Check for the presence of logical name table table LNM$DT_FORMAT_TABLE (e.g., do a SHOW LOGICAL /TABLE=LNM$DT_FORMAT_TABLE). If this table does not exist, request that the system manager include the command file SYS$STARTUP:LIB$DT_STARTUP.COM in the startup sequence (personally, my preference is to use the SYSMAN STARTUP database to do this, but one can also include it in SYS$MANAGER:SYSTARTUP_VMS.COM).

Then, before doing the DIRECTORY command, follow the instructions in the LIB Run Time Manual for selecting the apppropriate date/time format (recommendation: YearMonthDay HourMinuteSecond Hundredth) and do the DIRECTORY/OUTPUT/NOHEAD/NOTRAIL followed by the appropriate SORT command).

The date/time formatting support is quite extensive, and for that matter, extensible to support the multitude of national date/time formats. It is also possible do define one's own format, for processing purposes.

- Bob Gezelter, http://www.rlgsc.com
Jon Pinkley
Honored Contributor

Re: How to find latest created file in VMS ?

This is a pet peeve of mine that VMS can not do something as simple as sorting the directory listing (in stock VMS out of the box) without jumping through hoops. Yes, you can set a sortable directory listing if you have @SYS$STARTUP:LIB$DT_STARTUP in your system startup, and you issue a command like

$ define lib$dt_format LIB$DATE_FORMAT_037,LIB$TIME_FORMAT_001

but that is a kludge at best.

And there is QDSB that is similar in concept to RSX's SRD sorting features (doesn't have the ability to display files that would be deleted by a purge/ver=x; SRD's Obsolete Versions output). But that doesn't exist out of the box, so you can not "assume" it will be there.



I thought a unix i-node was analogous to a VMS file header, and that a unix directory was a special file that contained names and i-node numbers, very similar in concept to a VMS directory. VMS directories are more constrained because they are stored in sorted order and must be contiguous, and has things the unix directory does not, e.g. the version limit for a filename.

While it is true that getting anything but the filename will result in having to get the information from the file header, the same is true for unix (although the unix buffer cache may make a trip to the disk less likely). For whatever reason, VMS engineering didn't think sorting by anything other than name was important, and I my opinion, that was a mistake.

Think about it, if we do a directory/size or directory/own, or directory/date we must already fetch the file header, so why not allow the directory to be sorted.

In RSX, the directories were not stored in alphabetical order, thus SRD (sorted directory) was quickly written, and it could sort by more than just the file name, it could sort by dates as well. (See QSDB as a VMS utility with similar sorting functionality).

Even MS-DOS can sort directories.

> dir /o:d (or /o:-d for newest files first)

will sort in date order.

That VMS has never enhanced directory to provide at least what the RSX SRD utility did, and never provided auto synchronization of VMS$MAIL_PROFILE's unread mail counter with mail.mai is an embarrassment. How many times have you had to answer "MAIL says I have 3 new messages, but there aren't any"? Expecting users to read the VMS FAQ is not the answer.

Claiming that either of those was never done because it was too inefficient is an excuse that has been obsolete for the last 15 years. Seriously, to fix the mail problem for the most noticeable case, where VMS tells you have unread mail when in fact you do not, would only require a keyed read of mail.mai to see if there is a least one record in the NEWMAIL folder (for interactive processes anyway). Most people don't care (and would never notice) if they are told they have 48 new messages when in fact they only have 45, but they will notice when they are told they have new mail, but when they enter mail, they do not.

Sorry for the tangent... zero points.

it depends
Jim Geier_1
Regular Advisor

Re: How to find latest created file in VMS ?

I too am frustrated by OpenVMS not being able to sort the output of a DIRECTORY command by date and time. I put together a rather crude DCL script to do this using the collating sequence capability in the SORT utility. The DCL script has many limitations, and may serve as the basis for building your own.

see attached file, sort_by_date.txt
Jon Pinkley
Honored Contributor

Re: How to find latest created file in VMS ?

For Sk Noorul Hassan's specific question, Hein's reply from Jan 4, 2008 14:00:58 GMT with the file specification changed from

$ file = F$SEARCH("[...]*.tmp;*")


$ file = F$SEARCH("[...]*.*;*")

should do exactly what was asked for.

However, VMS Directory should have the ability to sort by many things, including the file dates. Even sorting by owner or protection could be useful. And sorting the output of directory is not the correct solution, there are just too many things that must be considered, the most obvious is the problem of long file names causing wrapping. Yes, there are ways to avoid most of the problems, but why shouldn't VMS directory be able to do this straight out of the box?

P.S. in my last response, I used the incorrect name for a unix file header. It is inode, not i-node. The unix analogue of a VMS FID is the unix inode number.
it depends
Regular Advisor

Re: How to find latest created file in VMS ?

About how many files per day are being created in that directory?

If you don't want to do a command file, you can get a list of recent files like this:

$ dir/since/size/date
This would be all files from today
$ dir/since=12:00/size/date
That would be all of the files since noon. Or you could put any time in the /since= qualifier.

$ dir/since=19:44/size/date
Or whatever time you want.

So if this narrows down the number of files you can see on, say, one screen, you can easily see which files were created last.

Also, $ dir/date=all or dir/modified will tell you when the file was modified if it was not necessarily created today.

You might want to check out
$ help directory

for all of the command qualifiers available to DIR.