Previous Page | Next Page

Using External Files

Reading and Modifying Files in the Same Directory with SCL

To assign a fileref to a directory, use the FILENAME function, just as you would to assign a fileref to a file. Before you can perform operations on multiple files in a directory, you must open the directory, just as you open an external file. To open a directory, use the DOPEN function.


Determining the Number of Files in a Directory

To find out the name of a file in a directory, you must know the number of files in the directory. You can determine that number by using the DNUM function.

In a program that displays the filenames in an extended table or in an SCL list, you use the value returned by the DNUM function to determine how many rows to display in the extended table or list.


Finding the Names of Files

After you find the number of files in the directory, you can use the DREAD function to read their names.

If you are using DREAD in a program that is not for an extended table, put the function in a DO loop so that it processes from 1 to the value returned by the DNUM function, as follows:

dirid=dopen(fileref);
numfiles=dnum(dirid);
do i=1 to numfiles;
   name=dread(dirid,i);
...more SCL statements...
end;


Manipulating Files in an Open Directory

When you open and close files in a directory that you opened with the DOPEN function, you can manipulate any of the files without assigning a fileref to each file. To use this technique, you must


Opening Files in an Open Directory

To open a file in a directory that you opened with the DOPEN function, use the MOPEN function. This function returns a file identifier. You can use this identifier with any function that uses a file identifier value returned by the FOPEN function. That is, you can use any SCL file function on any file that you have opened with the MOPEN function. For example, when you open files with the MOPEN function, you use the FCLOSE function to close the files.


Closing Files in an Open Directory

When your program is finished with a file in an open directory, you must close that file. To close a file, use the FCLOSE function.


Changing All the Files in a Directory

When you use the directory and file functions, you can create applications that enable users to make a change in each file in a directory. For example, you might want to change a date or multiply all the salaries by the same percentage when everyone represented in the file receives the same percentage raise in pay.

To make the same change to all the files in a directory, first pass the directory name to the FILENAME function, and then use the DOPEN function to open the directory. Then, follow these steps:

  1. Use the DNUM function to return the number of files in the directory. Use the number as the end of a DO loop that processes each file.

  2. Use the DREAD function to read the name of a file for each repetition of the loop.

  3. Use the MOPEN function to open the file.

  4. Use the FREAD function to read a record from the file.

  5. Use the FPOS function to move the FDB column pointer to the value's start column.

  6. Use the FGET function to copy data from the File Data Buffer (FDB) and to assign it to the specified character variable.

  7. Use the FPOS function to return the FDB column pointer to the value's start column.

  8. Use the FPUT function to write the modified value back to the FDB.

  9. Use the FWRITE function to write the modified record back to the external file.

  10. Use the FCLOSE function to close the file at the end of the processing loop.


Creating a Subdirectory

You can use the DCREATE function to create a subdirectory.


Closing a Directory

When your application is finished with the files in a directory, you should close the directory. To close a directory, use the DCLOSE function.

CAUTION:
Be careful to complete operations on all files before closing the directory.

When you use the MOPEN function to open files, be sure your program completes its operations on all the directory's files before you use the DCLOSE function. When you use the DCLOSE function, SCL closes all the directory's files that were opened previously with the MOPEN function.  [cautionend]


Other Manipulations for Directories

The following SCL functions provide additional information about directory attributes:

DOPTNUM reports the number of directory attributes available for a file.
DOPTNAME returns the name of a directory attribute for a file.
DINFO returns the value of a directory attribute for a file.

Previous Page | Next Page | Top of Page