Skip to main content

Is there a way in RM/COBOL to check a size of a file before opening it?  Having an issue with a rather large log file - want to change the program to delete it and create a new one if the file size is larger that 1GB.

Is there a way in RM/COBOL to check a size of a file before opening it?  Having an issue with a rather large log file - want to change the program to delete it and create a new one if the file size is larger that 1GB.

Hi AmyM, I don't know any way to do this with native RM/COBOL. But you can make a .dll with CodeBridge. An example of this


getsize.tpl

#include "getsize.h"

[[ integer out ret_val]] int getsize(
[[ string in]] char *path);

getsize.h

#include <stdio.h>

int getsize(char *path)
{
int size = -1;
FILE *fd = fopen(path, "rb");
fseek(fd, 0L, SEEK_END);
size=ftell(fd);
fclose(fd);

return size;
}

and then in your cobol program

...
...
01 SIZE PIC 9(10).
...
...
CALL "getsize" USING
"C:\\FILE.DAT"
GIVING SIZE.

I wish it can solve your problem.


Is there a way in RM/COBOL to check a size of a file before opening it?  Having an issue with a rather large log file - want to change the program to delete it and create a new one if the file size is larger that 1GB.

Thanks! I think that might work :)

Is there a way in RM/COBOL to check a size of a file before opening it?  Having an issue with a rather large log file - want to change the program to delete it and create a new one if the file size is larger that 1GB.

Hi Amy, I used to do this in native RM in a very low tech way, but the technique can also be used to find other file/folder information and system information too. I no longer have access to a compiler so cannot give a working example, but idea is as follows:

Use native CALL “SYSTEM” to do a “dir” or “ls” (according to OS) on the file and redirect the output (using >) to a uniquely named file (date/time to hundreds of second?). Then from within the COBOL program read the created sequential file back in and extract the file size from the appropriate line of the file.

I wrapped this code into a subroutine which would detect the OS and then issue the appropriate command and read back the file looking for the response in the right place.

The technique can also return date/time of last update, files in a folder, etc.

I hope this helps.

Cheers

Nigel


Is there a way in RM/COBOL to check a size of a file before opening it?  Having an issue with a rather large log file - want to change the program to delete it and create a new one if the file size is larger that 1GB.

Hi AmyM,

I apologize for the late response. I have a utility program I use to verify file size which can run on Windows, Unix, or Linux. In it's current state, it prints a report. However, you could modify paragraph "290-PROCESS-SORT-FILE" to delete the file.

I will try to attach it here (if I can figure that out).GETFILESIZE.zip