Operating System - OpenVMS
1752696 Members
6155 Online
108789 Solutions
New Discussion юеВ

Testing the size of a file

 
SOLVED
Go to solution
Gordon  Morrison_1
Regular Advisor

Testing the size of a file

Frustrated Unix guy here. Here's an easy one for you:
How can I test the size of a file in a DCL script using IF/THEN/ELSE/ENDIF ?

I basically want to do this:

IF (size of file.txt) .GT. 0
THEN
do_this
ELSE
do_that
ENDIF

Thanks
What does this button do?
6 REPLIES 6
Hoff
Honored Contributor

Re: Testing the size of a file

Do not remember anything about bash when you're working with DCL; the two are very different, and you'll find DCL exceedingly frustrating if you try to map what you can do in bash over to OpenVMS...

$ if f$file(FileNameInASymbol,"EOF") .gt. 0
$ then
$ write sys$output "Stuff"
$ else
$ write sys$output "No Stuff"
$ endif


Alternatively:

$ write sys$output "File size: " + f$file("SYS$LOGIN:LOGIN.COM","EOF")
13
$


Logical names and symbols will confuse you, as will the vast differences in the process model; you'll need to sort that out.

Lexical functions are the rough analog of some of the built-in stuff in the bash shell.

Here's some related mappings of shell and DCL commands:

http://labs.hoffmanlabs.com/node/741

As for recommended reading, see the OpenVMS User's Guide and the DCL dictionary in the OpenVMS documentation set:

http://www.hp.com/go/openvms/doc
Bojan Nemec
Honored Contributor
Solution

Re: Testing the size of a file

Gordon

You can use the lexical function F$FILE to get the size of the file:

F$FILE("file.txt","EOF")

gives you the number of blocks used in the file.

F$FILE("file.txt","FFB")

gives you the first free byte in the last block.

(F$FILE("file.txt","EOF") - 1) * 512 + F$FILE("file.txt","FFB")

gives you the file size in bytes.
Yours example will probably be something like this:

IF F$FILE("file.txt","EOF") .GT. 0
THEN
do_this
ELSE
do_that
ENDIF



Bojan
Gordon  Morrison_1
Regular Advisor

Re: Testing the size of a file

Thanks Bojan. That's exactly what I needed.
What does this button do?
Gordon  Morrison_1
Regular Advisor

Re: Testing the size of a file

Question answered by Bojan
What does this button do?
John Gillings
Honored Contributor

Re: Testing the size of a file

Gordon,

A caveat on using F$FILE attribute EOF... if the file is currently open, and being written the EOF may not be up to date, so EOF could potentially be 0, even if you consider the file to contain data. For structured (indexed) files EOF doesn't mean much, so may not make sense.

Look at your candidate files, and compare different attributes to make sure you're getting results you want. As Bojan has suggested EOF and FFB are good for calculating the size of a sequential file, but you may be more interested in ALQ (allocated size). For example, a new file being written by another process may have EOF=0, but if any data has been written ALQ will be non-zero.

See

$ HELP LEX F$FILE ARG

for a list of file attributes you can test.
A crucible of informative mistakes
Martin Vorlaender
Honored Contributor

Re: Testing the size of a file

>>>
(F$FILE("file.txt","EOF") - 1) * 512 + F$FILE("file.txt","FFB")

gives you the file size in bytes.
<<<

This only holds if the FFB is .NE.0 . For FFB.EQ.0, the size in bytes is F$FILE("file.txt","EOF") * 512 .

cu,
Martin