Getting file size information

Chris Kelleher

Administrator
Staff member
How can I obtain a file size information in UNIX, except via LS -L and CUT command.
About a file information, the PROGRESS has a command called FILE-INFO, but it have nothing about SIZE of a file.
 

Chris Kelleher

Administrator
Staff member
This should work:

input from "c:\command.com".
seek input to end.
message seek(input) view-as alert-box information.

HTH.
 

Chris Kelleher

Administrator
Staff member
You made an enquiery about getting a file size from a Unix ls -l statement, well it depends if tou want a specific file or the whole directory.

Below is a typical entry from a HP-UX ls -l command.

-rwxrwxw-rw- 1 dba users 1094 Jul 10 11:51 start.sh

As you can see it basically compirses of 9 white-space seperated columns, the file size being in column 5 and the file name in column 9. so to get the file size of that file this should work.


DEFINE VARIABLE w-in AS CHARACTER FORMAT "x(25)" NO-UNDO.
DEFINE VARIABLE w-size AS INTEGER NO-UNDO.

INPUT THROUGH "ls -l | grep 'start.sh'" NO-ECHO.

SET ^ ^ ^ ^ w-in.

INPUT CLOSE.

ASSIGN w-size = INTEGER(w-in).


That should give you the size of the file start.sh in the variable w-size. You will obviously have to replace the 'start.sh' operand of the Unix grep command with the name of the file you are looking for. If you wanted to collect the sizes of the whole directory along with the file names then the following example should provide your solution.

DEFINE VARIABLE w-in AS CHARACTER FORMAT "x(25)" EXTENT 2 NO-UNDO.

DEFINE TEMP-TABLE tt-info NO-UNDO
FIELD file-name AS CHARACTER
FIELD file-size AS INTEGER.

INPUT THROUGH "ls -l" NO-ECHO.

Process-Info:
REPEAT:

SET ^ ^ ^ ^ w-in[1] ^ ^ ^ w-in[2].

CREATE tt-info.

ASSIGN tt-info.file-size = INTEGER(w-in[1])
tt-info.file-name = TRIM(w-in[2]).

END.

INPUT CLOSE.


Assuming you were in the correct directory when you ran these examples you should have the info you want, in the case of the second example a complete list of all files with their sizes held in temp-table tt-info. This technique can be applied to any listing really with minor adaptations.

Note: The ^ (carat) character is used to skip a field that is not required to be imported.

I hope these examples will be of some help.

Rob.B.
 

rpenridge

New Member
One other thing to note... if you have two files in a directory rob.1 and rob.11 the command:

ls -l | grep 'rob1'

would return:

-rwx------ 1 rpenridg users 9880 Mar 1 12:57 rob.1
-rwx------ 1 rpenridg users 10169 Mar 1 12:36 rob.11

To make sure that you only get the exact file you are after use the following command instead:

ls -l | grep 'rob1$'

The $ character means EOL I think... in other words we're using grep to search for the string 'rob.1' right before the end of the line. The output to the above command will just be:

-rwx------ 1 rpenridg users 9880 Mar 1 12:57 rob.1

I don't know if this works in all flavours of UNIX...

Cheers,
Rob
 
B

Bert Naik

Guest
Byte count of Unix file

Try the Unix Word Count command:

wc -c

or

wc -m
 
Top