Table of Contents

Scanning Directories

You can use the file service to scan your directories for files and subdirectories. In the file service codeunit, we have the ScanDir function.

procedure ScanDir(Path: Text, SearchPattern: Text, Recursive: Boolean, var ForNavFileDirectory: Record "ForNAV File Directory" temporary): Boolean

The Path parameter tells which folder to scan for files, and the SearchPattern specifies which files to look for.

Recursive scans will include subfolders and their files.

ScanDir returns the result in a ForNavFileDirectory temporary table.

Example

This is an example of how to use the ScanDir function. The code will get all the files and subfolders under the DEMOFILES alias.

trigger OnAction()
var
    TempFileDirectory: Record "ForNAV File Directory" temporary;
    FileService: Codeunit "ForNAV File Service";
begin
    FileService.ScanDir('DEMOFILES:\', '*.*', true, TempFileDirectory);
    if TempFileDirectory.FindFirst() then;
    TempFileDirectory.SetFilter(ShareDirectory, TempFileDirectory.LinkDirectory);
    page.RunModal(Page::"ForNAV File Directory", TempFileDirectory);
end;

After receiving the result, the files are shown in the ForNAV File Directory page.

ScanDir Page

List aliases

You can get a list of available aliases on a file service. All you have to do is leave the Path parameter blank.

Limitations

Some directories contain a lot of files or subfolders. You may hit a limit on how many files or folders this function can handle. You can turn off recursive scanning or split the files into multiple subfolders to overcome this problem.

File security

If you scan a directory structure where some folders are inaccessible to the service user, the scan operation will fail for versions before 7.3.0.2547. After that, inaccessible folders will be ignored and appear empty.

Batch mode

Directories can be scanned in batch mode using the functions ScanDirTask and GetScanDirTaskResult.

Search patterns

When you scan a directory, you can specify a search pattern to determine which files and directories are included in the result.

Different patterns are supported to give you flexibility in how you want to search and filter.

Standard patterns

The standard pattern includes your operating system's *.* syntax. To find all PDF documents, you specify *.pdf as the pattern.

Wildcard characters * and ? are used in the pattern to widen the search.

  • * - represents any string or character in the file or directory name.
  • ? - represents a single character.

More information on how to use wildcard characters is available at Microsoft Learn.

If you specify a search pattern, it will only apply to files. By default, directories are not filtered. Directories can be filtered by writing two patterns separated by the | character. The pattern a*.*|*.png will find all directories starting with a and files with the png extension.

Multiple patterns

You can search for files of different types by separating the standard patterns with the ; character. A typical scenario is finding image files with patterns such as *.gif;*.png;*.bmp.

Regular expression

If you cannot formulate a standard pattern that meets your criteria for filtering files and folders, you can experiment with regular expressions. Most people find regular expressions hard to master, but they are an extremely powerful tool.

The file service will use your pattern as a regular expression if it starts with the @ character.

Files and folders are filtered using the same regular expression. The expression is tested against the full path of the file name. Folder names end with a backslash \. The backslash at the end can be used to match file names and folder names precisely.

Here are some examples:

Pattern Matches
.* All files and folders
\\$ All folders
[^\\]+$ All files
a[^\\]*$\|\\$ All files starting with a or A in any folder
(?-i:)a[^\\]*$\|\\$ All files starting with a in any folder

Patterns are case-insensitive by default. The use of (?-i:) turns ignore case off.

FileService.ScanDir('', '@a[^\\]*$\|\\$', true, TempFileDirectory);