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

List the latest file

 
SOLVED
Go to solution
Per Arvidsson
Occasional Visitor

List the latest file

In DCL how can I get the latest created file on a directory?
16 REPLIES 16
Joseph Huber_1
Honored Contributor
Solution

Re: List the latest file

You can write a DCL command file:

lastfile=""
last="17-NOV-1858"
last=f$cvtim(last,"COMPARISON")

Loop with file=f$search("*.*") over the directory,
get the creation date cdt=f$file(file,"CDT")
convert the date to comparison cdt=f$cvtim(cdt,"COMPARISON")
Compare to the last time saved
Set last to cdt if cdt .ges. last
Set lastfile to file in this case
Continue loop.

Or search on the VMS freeware disks for the program DIRBYDATE: it does not give You just the latest, but a directory listing with the latest first or last in the list.
http://www.mpp.mpg.de/~huber
Hakan Zanderau ( Anders
Trusted Contributor

Re: List the latest file

Per,

What are you trying to do ?
We need more detailed information.

Hakan

Ps. Jobbade tidigare på VMS-supporten hos Digital/Compaq/HP. Ds.
Don't make it worse by guessing.........
Kris Clippeleyr
Honored Contributor

Re: List the latest file

Per,

You could use QDSB, downloadable from
http://www.quadratrix.be/qdsb.html

Example:
$ QDSB/SORTED=DATE_C=ORDER=DESC

Kris (aka Qkcl)

I'm gonna hit the highway like a battering ram on a silver-black phantom bike...
Per Arvidsson
Occasional Visitor

Re: List the latest file

Thank you all, Wow!
What a response
Per Arvidsson
Occasional Visitor

Re: List the latest file

I tried to pipe dir and sort but didnt succeed. These answers will do
Joseph Huber_1
Honored Contributor

Re: List the latest file

I think Kris's program is the most excellent doing a sorted directory anyway.

>> tried to pipe dir and sort but didnt succeed.

Because the default date format is not ISO or VMS "comparison": one has to switch date-format before the directory command, and has to fix the output fields with the /width qualifier, then one can pipe the output through sort.

I did this for the file size (because it's simpler :-), see
http://wwwvms.mppmu.mpg.de/util_root/com/dir_by_size.com

http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: List the latest file

A quick hack in DCL piping directory output into sort is at
http://wwwvms.mppmu.mpg.de/util_root/com/dir_by_date.com

but it will break the correct order and display whenever a dir-/file-specification exceeds the filename field width (80 in my example).

A slower DCL version using f$search loop is
http://wwwvms.mppmu.mpg.de/util_root/com/dir_by_date2.com

http://www.mpp.mpg.de/~huber
Hakan Zanderau ( Anders
Trusted Contributor

Re: List the latest file

$ DEFINE/EXEC/TAB=LNM$DT_FORMAT_TABLE LIB$DATE_FORMAT_COMPARISON "!Y4-!MN0-!D0"
$ DEFINE LIB$DT_FORMAT LIB$DATE_FORMAT_COMPARISON,LIB_TIME_FORMAT_001
$ pipe (dir/date/wid=file=40 | search sys$input ";" | sort/key=(pos:43,siz:22,desc) s
ys$input sys$output)

Hakan
Don't make it worse by guessing.........
Joseph Huber_1
Honored Contributor

Re: List the latest file

BTW, the LIB$DT_FORMAT feature only works if
@sys$startup:lib$dt_startup has been executed (best insert this in systartup_vms ).
http://www.mpp.mpg.de/~huber
Hakan Zanderau ( Anders
Trusted Contributor

Re: List the latest file

Joseph,

The default date & time formats

LIB$DATE_FORMAT_001
LIB$TIME_FORMAT_001

are defined in SYS$STARTUP:VMS$INITIAL-050_LIB.COM

You can define your own date and time format
in table lnm$dt_format_table and make use of it without running LIB$DT_STARTUP.COM

I think I'm right about this, but I'm not 100% sure.

Hakan
Don't make it worse by guessing.........
Joseph Huber_1
Honored Contributor

Re: List the latest file

As far I tested, only privileged users can do it.
So for average users it is better to rely on definitions done from systartup_vms.
http://www.mpp.mpg.de/~huber
Hakan Zanderau ( Anders
Trusted Contributor

Re: List the latest file

Joseph,

Oppps....forgot about the privileges needed to
define names in lnm$dt_format_table.

The truth is that I have never worked without
privileges on VMS........ and have all privileges enabled by default.

I know...., but that's the way it is ;-)

Hakan
Don't make it worse by guessing.........
Steven Schweda
Honored Contributor

Re: List the latest file

> You can write a DCL command file:

You're right. This came up in the Linux
forum:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1335784

After saying that it would be easy in DCL, I
figured that I ought to try it.

$! 8 May 2009. SMS.
$!
$! Find newest file in (wildcard) P1 specification.
$!
$ prev_file_spec = ""
$ date_max_c = f$cvtime( "17-NOV-1858 00:00:00.00", "COMPARISON")
$ file_spec_max = ""
$!
$ loop_top:
$!
$ file_spec = f$search( p1)
$!
$! Quit if out of files, or (non-wildcard) look-up repeats.
$!
$ if ((file_spec .eqs. "") .or. -
(file_spec .eqs. prev_file_spec)) then goto loop_end
$!
$ prev_file_spec = file_spec
$!
$! Get file revision date, and convert it to "COMPARISON" format.
$!
$ file_date = f$file_attributes( file_spec, "RDT")
$ file_date_c = f$cvtime( file_date, "COMPARISON")
$!
$! Save the information for a new newest file.
$!
$ if (file_date_c .gts. date_max_c)
$ then
$ file_spec_max = file_spec
$ date_max_c = file_date_c
$ endif
$!
$ goto loop_top
$ loop_end:
$!
$! If anything was found, display its information.
$!
$ if (file_spec_max .nes. "")
$ then
$ write sys$output file_spec_max
$ write sys$output date_max_c
$ endif
$!

Easy enough, I reckon.
Joseph Huber_1
Honored Contributor

Re: List the latest file

And here (without own programming), let the fine GNV do the work:-)

$ bash -c "ls -lt"
http://www.mpp.mpg.de/~huber
Steven Schweda
Honored Contributor

Re: List the latest file

> $ bash -c "ls -lt"

That gives me more than "the [...] file".
Or did you mean this?:

$ bash -c "ls -lt | head -2 | tail -1"

The DCL method runs faster, and easily does
recursive descent into subdirectories
("[...]"), which is tougher to do with a
UNIX-like method.

As usual, many things are possible.
Joseph Huber_1
Honored Contributor

Re: List the latest file

>>That gives me more than "the [...] file".
>>Or did you mean this?:
>>$ bash -c "ls -lt | head -2 | tail -1"

Yea right, thats exactly to get the one newest file, as Your DCL command file is doing.

I was just comparing the bash one liner to my dir_by_date.com .
http://www.mpp.mpg.de/~huber