#

| Chapter 13 PLVfile: Reading and Writing Operating System Files |

## 13.2 Specifying the File in PLVfile

Now that you are aware of the way that UTL_FILE works, let's look at how PLVfile makes it easier to use the builtin package.

First of all, rather than insist that you separate out the file location from the file name to open and manipulate files, PLVfile provides a set of programs to make it easier to specify files. These programs are discussed below.

### 13.2.1 Setting the Operating System Delimiter

Each operating system has a delimiter that it uses to separate out directories and subdirectories, as well as separating directories from file names. Since PLVfile allows you to specify a file name as a single string (directory and file name combined), it needs to know about the operating system delimiter.

Use the `set_delim` to set the operating system delimiter. Its header is:

>     PROCEDURE set_delim (delim_in IN VARCHAR2);

You can find out the current operating system delimiter by calling the `delim` function:

>     FUNCTION delim RETURN VARCHAR2;

The PLVfile package offers two predefined delimiters for UNIX and DOS as shown:

>     c_unixdelim CONSTANT VARCHAR2(1) := '/';
>     c_dosdelim CONSTANT VARCHAR2(1) := '\';

The default, initial setting for the OS delimiter is the UNIX delimiter: "/".

### 13.2.2 Setting the Default Directory

PLVfile maintains a current directory so that you do not have to continually specify a directory if you are always working in the same area on disk. To set the current directory, call the `set_dir` procedure. To determine the current setting for the directory, call the `dir` function. The headers for these programs are:

>     PROCEDURE set_dir (dir_in IN VARCHAR2);
>     FUNCTION dir RETURN VARCHAR2;

The following call to `set_dir` sets the default directory to a path in DOS:

>     SQL> exec PLVfile.set_dir ('c:\orawin\oe_app');

> NOTE: If you do not call `PLVfile.set_dir` before passing in file names for reading and writing, there is a very good chance that your efforts to use PLVfile will be very frustrating. You will get errors that are difficult to understand, since you know your file exists. One way to minimize the frustration is to place a call to `PLVfile.set_dir` in your `login.sql` script.

Notice that I do not include a terminating backslash in the string. That "final" delimiter is needed when attaching the directory to the file name, but is neither needed nor legitimate for specifying a directory. In fact, if you include a final delimiter, PLVfile will strip it from the string, as shown below:

>        PROCEDURE set_dir (dir_in IN VARCHAR2)
>        IS
>        BEGIN
>           v_dir := RTRIM (dir_in, v_delim);
>        END;

### 13.2.3 Parsing the File Name

PLVfile allows you to provide the file name as a single string. When you do this, PLVfile calls `parse_name` to parse the string into its separate components. The header for `parse_name` is:

>     PROCEDURE parse_name
>        (file_in IN VARCHAR2, loc_out IN OUT VARCHAR2,
>         name_out IN OUT VARCHAR2);

where `file_in` is the full file specification (location, name, and extension). The `loc_out` argument receives just the directory, while the `name_out` argument receives the name and extension. It relies on the operating system delimiter you assigned with a call to `set_dir` in order to find the start of the file name.

If the string you pass to `parse_name` does not have a directory prefixed on the file name, PLVfile will return the default directory as the location.

The following table shows how `parse_name` parses and returns values:

parse_name | Default Directory | File Location Returned | File Name Returned
--- | --- | --- | ---
/usr/app/names.lis | NULL | /usr/app | names.lis
/usr/app/names.lis | /oracle/prod/defdir | /usr/app | names.lis
names.lis | NULL | NULL | names.lis
names.lis | /oracle/prod/defdir | /oracle/prod/defdir | names.lis

This procedure is used extensively inside PLVfile (see Section 13.4, "Opening and Closing Files" for an example of how `parse_name` is used to overload several different versions of `fopen`). You can, however, also call `parse_name` directly in your own application. Just make sure that you have set the OS delimiter before you use `parse_name`.

13.1 A Review of UTL_FILE |  | 13.3 Creating and Checking Existence of Files

Copyright (c) 2000 O'Reilly & Associates. All rights reserved.