DEV Community

Peter + AI
Peter + AI

Posted on

πŸ“‚ Mastering Directory Listing in Uniface: A Complete Guide to $dirlist Function

πŸ€– This blog post was created with AI assistance to provide clear and comprehensive information about Uniface programming.

Working with file systems is a common task in enterprise applications, and Uniface 10.4 provides powerful built-in functions to handle directory operations efficiently. Today, we'll explore the $dirlist function, which allows developers to retrieve directory contents programmatically. πŸš€

πŸ” What is $dirlist?

The $dirlist function is a built-in Uniface function that returns the contents of a specified directory. Think of it as the programming equivalent of the ls command in Unix or dir command in Windows. It's particularly useful for building file management features, creating dynamic file processing workflows, or implementing backup systems.

πŸ“ Basic Syntax

The function follows this simple syntax:

$dirlist(DirPath {, Topic})
Enter fullscreen mode Exit fullscreen mode

Parameters Explained πŸ“‹

  • DirPath (String): The directory path you want to examine. Can include wildcards like ? (single character) or * (multiple characters)
  • Topic (String, optional): Specifies what to return:
    • "FILE" - Lists files (default behavior)
    • "DIR" - Lists subdirectories only

πŸ’‘ Practical Examples

Example 1: List All Files πŸ“„

variables
 string vContent
endvariables

vContent = $dirlist("C:/projects/", "FILE")
putmess "Files found: %%vContent%%"
Enter fullscreen mode Exit fullscreen mode

This example retrieves all files from the C:/projects/ directory and displays them.

Example 2: List Subdirectories Only πŸ“

variables
 string vDirectories
endvariables

vDirectories = $dirlist("C:/workspace/", "DIR")
putmess "Subdirectories: %%vDirectories%%"
Enter fullscreen mode Exit fullscreen mode

Example 3: Using Wildcards 🎯

variables
 string vLogFiles
endvariables

vLogFiles = $dirlist("C:/logs/*.txt", "FILE")
putmess "Log files: %%vLogFiles%%"
Enter fullscreen mode Exit fullscreen mode

This example finds all text files in the logs directory.

Example 4: Processing Results Line by Line πŸ”„

Here's a more comprehensive example that processes each file individually:

variables
 string vFilePath, vContent
 numeric N
endvariables

vContent = $dirlist("C:/data/", "FILE")
putmess "Processing files in C:/data/:"

N = 1
getitem vFilePath, vContent, N
while ($status > 0)
 putmess "Processing: %%vFilePath%%"
 ; Add your file processing logic here
 N = N + 1
 getitem vFilePath, vContent, N
endwhile
Enter fullscreen mode Exit fullscreen mode

🎯 Common Use Cases

1. Backup Systems πŸ’Ύ

Use $dirlist to identify files that need backing up based on modification dates or file patterns.

2. File Processing Workflows πŸ”§

Automatically process batch files, logs, or data imports by scanning specific directories.

3. Dynamic File Management πŸ“Š

Build administrative interfaces that allow users to browse and manage files within your application.

4. Report Generation πŸ“ˆ

Scan directories for data files to include in automated reporting systems.

⚠️ Error Handling

Always check for errors when using $dirlist. The function returns an empty string if the directory doesn't exist or an error occurs. Use $procerror to get detailed error information:

variables
 string vResult
endvariables

vResult = $dirlist("C:/nonexistent/", "FILE")
if (vResult = "")
 putmess "
Enter fullscreen mode Exit fullscreen mode

Top comments (0)