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

DCL get number of files in a directory

SOLVED
Go to solution
Steven Watson
Occasional Contributor

DCL get number of files in a directory

This is for DCL script.

Is there a system function (Lexical/Lib/Services) to get the number of files in a directory like "$ DIR/GRAND directoryname" DCL command?

I'm trying to avoid having to write output of DIR/GRAND to a file and then extract the info.

Thanks.
5 REPLIES
Jim_McKinney
Honored Contributor
Solution

Re: DCL get number of files in a directory

No - but you can do something like this:

$ pipe dir/grand | sear sys$pipe " files." | -
( read sys$pipe x ; x = f$elem(5," ",x) ; defi/job x &x )
$ x = f$inte(f$trnl("x","lnm$job"))
$ show symbol x
Richard W Hunt
Valued Contributor

Re: DCL get number of files in a directory

Concur with Jim.

Here's why...

The directory doesn't contain one record per file. It contains one record per file name and the record includes several sub-entries, one for each version. So you cannot count directory records. It is possible to have 1000 files in a directory but only one (very long) directory record holding one name, one type, and 1000 versions.

So there is nothing you can look at in the directory from a DCL program to tell you how many files are in it. Only a directory-aware program (like DIRECTORY.EXE) can do this for you. You could just "roll your own" program to read the directory and return the count, but why re-invent the wheel? Trap the output of the DIR command and get the answer. Don't avoid the easiest method just to avoid the overhead of creating, reading, and deleting a throw-away file.
Sr. Systems Janitor
Hein van den Heuvel
Honored Contributor

Re: DCL get number of files in a directory

If there is garantueed to be just 1 version, or less than 60 and you are only interested in the number of names, then you can use: $SEARCH/NOWARN/WIND=0/STAT x.dir WF*$$*78j23
For OpenVMS the SEARCH/STAT command will define DCL SYMBOL: SEARCH$RECORDS_SEARCHED

If you just want to count, then be sure to use a 'clear' DIR command, not an alias to avoid the fiel header IOs'. I tend to use a spelled out DIREctory or DIREx

Picking up the count form DRIECTORY/GRAND using a pipe is probably the most efficient.
But you _could_ figure it out with DCL:

$tot = 0
$num = 1
$open/read/share=write/error=done dir 'p1
$
$loop:
$read/end=done dir rec
$nam = f$cvui(3*8,8,rec)
$cnt = ( f$len(rec) - nam - 4 ) / 8
$tot = tot + cnt
$num = num + 1
$goto loop
$
$done:
$close/nolog dir
$write sys$output tot, " files counted in ", num, " records."

Cheers,
Hein.


Re: DCL get number of files in a directory

Or you can just count them...

$ d = "[.kit]*.*;*"
$ cnt = -1
$ next: cnt = cnt + 1
$ x = f$search( d )
$ if f$length( x ) .gt. 0 then goto next
$ write sys$output cnt

Simple, clear, slow...

jon
labadie_1
Honored Contributor

Re: DCL get number of files in a directory

Using awk is handy for such a job

Define a symbol for awk

$ awk :== $ sys$common:[syshlp.examples.tcpip.snmp]gawk.exe


$ pipe dir/gr dsa2:[xxx.exp.yyy] | awk/comm="{if (/files./) print $(NF-1) }" sys$pipe

gives this number

The output of the
$ dir/gra ...
command gives several lines, we are interested in the line with the text
files.
and we want to print the last but one word of this line
$NF is the last word
$(NF-1) is the last but one word.