Xceed .NET Libraries Documentation
Filters

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Basic Concepts > Filters

You can target specific files and folders to process using either basic filters or Filter classes.  

Basic filters

A basic filter is a filter that is constructed from a string or a filter built using a file attribute. The most common example of a basic filter would be a name mask on a group of files. 

For example, if you wanted to process only the TXT files found in a folder, you could simply use "*.txt" as a filter without needing to create a Filter class around the string.

VB.NET Copy Code
Dim files As AbstractFile() = myFolder.GetFiles( True, "*.txt" )
C# Copy Code
AbstractFile[] files = myFolder.GetFiles( true, "*.txt" );
You could also process all files that have the read-only attribute without creating a filter class:
VB.NET Copy Code
Dim files As AbstractFile() = myFolder.GetFiles( True, System.IO.FileAttributes.ReadOnly )
C# Copy Code
AbstractFile[] files = myFolder.GetFiles( true, System.IO.FileAttributes.ReadOnly );

Filter class

The Xceed.FileSystem namespace regroups seven Filter classes: the AttributeFilter class which filters files and folders according to their attributes,  the DateTimeFilter class which filters files and folders according to their dates and times, the NameFilter class which filters files and folders according to their names and the SizeFilter class which filters files according to their size. It also regroups three logical Filter classes: the AndFilter class which allows logical-and operations, the NotFilter class which serves to negate filters and the OrFilter class which allows logical-or operations. All of these classes derive from the base Filter class. 

If we revisit the basic filter example and want to process only the TXT files found in a folder, but this time using a Filter class, you would need to create an instance of the NameFilter class and pass it the name mask in it's constructor. This is the same thing that happens underneath when using basic filters.

VB.NET Copy Code
Dim files As AbstractFile() = myFolder.GetFiles( True, New NameFilter( "*.txt" ) )
C# Copy Code
AbstractFile[] files = myFolder.GetFiles( true, new NameFilter( "*.txt" ) );

When using the AttributeFilter, the DateTimeFilter and the NameFilter classes, it is also possible to specify if the filters will apply to files, folders or both. This is done by using the FilterScope enumeration in the constructor of the Filter class.

VB.NET Copy Code
Dim files As AbstractFile() = myFolder.GetFiles( True, _
                                                 New NameFilter( "doc*", FilterScope.File ) )
C# Copy Code
AbstractFile[] files = myFolder.GetFiles( true, new NameFilter( "doc*", FilterScope.File ) );

It is also possible to derive from the base Filter class to create customized Filter classes if needed.

Logical filters

The AndFilter, NotFilter and OrFilter classes are used to create custom logic operations around multiple filters. When more than one filter is used, they are combined by default with an AndFilter. This means that for an item to be processed, it must match all of the filters provided. 

For example, if we wanted to process all files that have the TXT or the EXE extension and that have a size greater than 5k, the following code could be used:

VB.NET Copy Code
Dim sizeFilter As New SizeFilter()
sizeFilter.MinSize = 5120

Dim files As AbstractFile() = myFolder.GetFiles( True, _
                                New AndFilter( New NameFilter( "*.txt|*.exe" ), sizeFilter ) )
C# Copy Code
SizeFilter sizeFilter = new SizeFilter();
sizeFilter.MinSize = 5120;

AbstractFile[] files = myFolder.GetFiles( true,
                               new AndFilter( new NameFilter( "*.txt|*.exe" ), sizeFilter ) );

The pipe (|) used in the constructor of the NameFilter class serves the same purpose as an OrFilter class.

Since the AndFilter class is used by default, the creation of a new AndFilter class around the NameFilter and the SizeFilter can be omitted:

VB.NET Copy Code
Dim sizeFilter As new SizeFilter()
sizeFilter.MinSize = 5120

Dim files As AbstractFile() = myFolder.GetFiles( True, _
                                                 New NameFilter( "*.txt|*.exe" ), sizeFilter )
C# Copy Code
SizeFilter sizeFilter = new SizeFilter();
sizeFilter.MinSize = 5120;

AbstractFile[] files = myFolder.GetFiles( true, new NameFilter( "*.txt|*.exe" ), sizeFilter );

Since the NameFilter class is used by default when using a basic string filter, the creation of a new NameFilter class around the string can be omitted:

VB.NET Copy Code
Dim sizeFilter As New SizeFilter()
sizeFilter.MinSize = 5120

Dim files As AbstractFile() = myFolder.GetFiles( True, "*.txt|*.exe", sizeFilter )
C# Copy Code
SizeFilter sizeFilter = new SizeFilter();
sizeFilter.MinSize = 5120;

AbstractFile[] files = myFolder.GetFiles( true, "*.txt|*.exe", sizeFilter );