π€ 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})
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%%"
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%%"
Example 3: Using Wildcards π―
variables
string vLogFiles
endvariables
vLogFiles = $dirlist("C:/logs/*.txt", "FILE")
putmess "Log files: %%vLogFiles%%"
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
π― 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 "
Top comments (0)