Hi Steve,
> I know the way in a CL or RPG IV to see if there is a file that exists
> in a specified path. But, I have a problem with a current project I'm
> working on, I will not know the full name of the file. I will only know
> the first 4 characters and the extension. Is there a way to identify
> the existence of a file or the use of a wild card (example: upld* or
> *.dat).
You can use the opendir(), readdir(), closedir() APIs to get a list of
files in a given directory. Will that work for you?
For example (this is off the top of my head, I didn't try compiling it):
/copy ifsio_h
D entry ds likeds(statds)
D based(p_entry)
D dirh s *
D found s n
/free
dir = opendir('/some/dir');
if (dir = *null);
// check errno to see what went wrong
endif;
found = *off;
p_entry = readdir(dirh);
dow (p_entry <> *null);
filename = %str(%addr(entry.d_name));
if (%len(filename)>4 and %subst(filename:1:4)='upld'));
found = *on;
leave;
endif;
p_entry = readdir(dirh);
enddo;
callp closedir(dirh);
if (found);
// there was at least one file starting with 'ulpd'
else;
// there wasn't
endif;
Personally, I'd take the preceding code and make it into a generic
subprocedure that any program can call to check if a file starts with
any group of characters (passing that group of chars as a parameter).
That way, you can reuse it for all future projects.
If you'd rather have a "wildcard" solution, you could combine the
opendir/readdir, etc APIs with the regular expression APIs. This would
provide a powerful pattern matching language.
I've written articles about that exact subject (IFS directory APIs
w/regular expression APIs). The articles include the code for a service
program that (once compiled, etc, on your system) makes this process
relatively painless. You just do something like this:
found = *off;
d = IFS_opendir('/some/dir': '^upld.*');
if (d >= 0);
found = IFS_readdir(d: file) > 0;
IFS_closedir(d);
endif;
That will set found to *ON if there's at least one file that starts with
ulpd. You could change the pattern to '.*dat$' to search for a file
that ends with 'dat' if you like.
Obviously, you'd need to understand how regular expressions work, since
they're not the same as the "wildcards" you're used to using in Unix or
MS-DOS. But, if you're interested in that article, it can be found here:
http://www.systeminetwork.com/article.cfm?id=50900
(You'll need a Pro or higher subscription to the SystemiNetwork.com web
site to read that article, sorry.)
There's a follow-up to that article that provides wrappers so that you
can do the same thing from a CL program, as well.
http://www.systeminetwork.com/article.cfm?id=50930
The wildcard pattern-matching setup that you're accustomed to is
normally part of the 'globbing' support in the operating system. On
Unix and MS-DOS, there are APIs that provide support for globbing in
applications. However, to the best of my knowledge, i5/OS does not
provide these APIs -- so you either have to use pre-written IBM programs
(such as the LS command in Qshell) or you have to write your own
globbing library. That's why I used regular expressions -- because I'm
too busy (or perhaps, too lazy) to write my own globbing library!
Of course, if you want the "quick and dirty" solution, you could always
use QShell instead of RPG...
As an Amazon Associate we earn from qualifying purchases.