Highlighted
Absent Member.
Absent Member.
2176 views

A way of determining if a file is available

Hi,

Using the example below is it possible to check the file / path exists and/or is available.

1) In order to produce an invoice I "import" a company logo to produce a pdf that gets printed.

2) the company logo is stored in the following location //192.168.123.456/Shared/Data/CompanyLogo.jpg

However if the path or filename doesn't exist then the program seems to hang for 1 minute or 2 before carrying on.

So my question is - is there a Cobol way to check the file or path exists first, which is hopefully quicker, before I try and attach it?

Many thanks

Neil.

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: A way of determining if a file is available

You might try using:

call "CBL_CHECK_FILE_EXIST"  
    using    filename
                 file-details
    returning status-code
end-call

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: A way of determining if a file is available

Thanks Chris I will give it a try.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: A way of determining if a file is available

Hi Chris,

Ok that doesnt work if the IP address of the server if not on the network or you dont have permissions i.e it just seems to "hang" for a while before it returns.

So if my file exists on 192.168.100.1

But I'm actually checking 192.168.100.2

Is there any way of checking that with out a delay?

Many thanks

Neil.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: A way of determining if a file is available

I'm afraid not - at least not in any simple fashion.

The file path you're using is a UNC path - it refers to a file on a remote system, accessed via SMB (Microsoft file sharing). The delay you're seeing is the SMB resolver attempting to open a connection to the specified IP address, establish a session, verify the credentials of the user account under which your program is running, request the share (in this case "Shared") named in the path, query the contents of that share, see if it contains the next level of the path ("Data"), open that as a directory, see if it contains an entry called "CompanyLogo.jpg", open that, and read its contents.

Nearly all of the time is spent before the final two steps (opening CompanyLogo.jpg and reading it), so there's essentially no difference in wall-clock time between simply opening the UNC path and using an API such as CBL_CHECK_FILE_EXIST.

There's no way to check for the existence of the file "without a delay" because the computer your application is running on doesn't know the answer. It has to communicate with the remote system, and that takes time. (A lot of time, frequently, in the case of SMB, which is a fairly dumb protocol; and it's running over TCP/IP, which by design keeps trying for a while if it doesn't succeed at first. TCP/IP was not intended for the impatient.)

There are ways to avoid some of this delay in some cases where you can tell you won't be able to get the file - for example by checking whether the destination host is reachable at all. There are ways to hide the delay, by retrieving the file in a background thread while your application is doing other work. There are ways to avoid taking the delay every time, by caching the file locally. But none of these are trivial.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.