The ConvertFolder method is used to convert files in the given folder and optionally any subfolders as well. As with the ConvertFile method, the conversion settings are passed as a profile, or through a custom list of settings. When converting a folder of files, all files are converted with the same conversion settings.

If an output location is provided the directory structure, including subfolders, will be maintained in the new location. This directory must exist before the call to ConvertFolder is made.

If an output location is not provided a new folder named .converted is created in the same location as the source file and all output files are saved there.

Filtering Files in the Folder

You can use the Filter and the ExcludeFilter arguments to specify what files in the folder you want to convert. The Filter is always applied to the directory contents first, then the ExcudeFilter is applied to that list of files to remove the unwanted files.

Hidden and system files are ignored, and the search pattern filters files based on a regular expression match of the long name of a file. The Filter defaults to all files in the folder (*.*) if String.Empty or null are passed. ExcludeFilter is ignored when String.Empty or null is passed.

Multiple filters can be combined using the pipe (|) character, such as *.doc|*.pdf to process only Word and PDF files. The table below lists some examples of filtering directory contents.

Filter

Exclude Filter

Action

*.pdf

String.Empty

Process only PDF documents.

*.*

*.tif|*.jpg

Process all documents except TIFF and JPEG images.

*.doc|*.docx|*.txt

Draft_*

Process all Word and Text documents except those starting with Draft_.

Sorting the Files for Pickup

Starting with Document Conversion Service 3.0.029, this method now includes the ability to order the files by name, date created or date modified when picking up files from the Input folder.

Configuring the Sort Mode and Order

Sort order defaults to name and ascending when picking up files. Files in the root of the input folder are picked up and sorted first. If sub folders are enabled, they are searched in alphabetical order. Any files in each sub folder are then sorted and returned. Uses the PNFileSortMode enumeration.

Note: Any sorting options applied only control the order in which the files are picked up from the directory. Sorting does not guarantee the order the files are processed in, only that files sorted to the top of the list are submitted for conversion first. A smaller file further down the list might finish before a larger file that was first in the list.

There are four sorting modes that can be used:

oNone - No ordering is used. Files are returned in the order they were given to us from the underlying file system.

oName - This is the default if the setting is not found or the value is incorrect. Files are sorted based on the full path name of the source file in the input folder.

oDateCreated - Files are sorted based on their creation date. For watch folders where files are dropped, a file can be moved or copied into the folder. If the files are moved into the Input folder they will retain their original created date. Copying a file into the Input folder wil set the created date to the time of the copy.

oDateModified - Files are sorted based on when they were last modified on the computer.

The order of the files is either Ascending or Descending. Uses the PNFileSortOrder enumeration.

oAscending - sorted the files from low to high: 0-9, A-Z.

oDescending - sorts the files from high to low: Z-A, 9-0.

Converting a Folder of Files

The code sample below will convert all files in the folder C:\Test\InputFiles\ except TIFF, JPEG and BMP images. Any subfolders will also be searched for files to convert. A sort order of DateCreated is set, meaning files created first will be submitted for processing first. This does not control the order in which the files are completed.

 

IList<PNConversionItem> results = new List<PNConversionItem>();

String strOutputFolder = @"C:\Test\Output";

 

// Directory must exist

if ( !Directory.Exists(strOutputFolder) )

{

    Directory.CreateDirectory(strOutputFolder);

}

 

// Convert the folder

results = PNConverter.ConvertFolder(@"C:\Test\InputFiles",

                                       true, // include subfolders

                                       "*.*", // filter

                                       "*.tif|*.jpg|*.bmp", // exclude filter

                                       strOutputFolder, // output folder

                                       true, // overwrite existing

                                       false, // remove file ext

                                       false, // create log

                                       "TIFF 200dpi OptimizedColor", // settings

                                       String.Empty, // extensison profile

                                       String.Empty, // MIME profile

                                       null, // User settings

                                       String.Empty, // not using remote conversion (DCOM)

                                       String.Empty, // use default working folder

                                       String.Empty,

                                       PNFileSortMode.DateCreated, // sort by created date

                                       PNFileSortOrder.Asccending); // A-Z, 0-9

 

 

The created files, in this case TIFF images, will be placed in the output folder C:\Test\Output and the directory structure maintained. The name of the original file, including the extension, is used as the base name for the new file. Files matching the ExcludeFilter were not converted.

 

Reading the Results Collection

When converting a folder of files a list of PNConversionItem objects will be returned, one for every file found to convert. Each PNConversionItem object contains information about the original conversion request and an internal PNConversionResult object that lists the results of the conversion. The results of the conversion can be a list of created files or a collection of error messages detailing why the file was not converted.

This code sample traverses the returns results from the above folder conversion and lists the files created.

 

if (results != null)

{

    int idx = 0;

    foreach (PNConversionItem item in results)

    {

        idx++;

        Console.WriteLine("*******************************");

        Console.WriteLine(String.Format("* Item {0}                      *", idx));

        Console.WriteLine("*******************************");

                    

        if (item != null)

        {

            Console.WriteLine("Item: " + item.SourceFilePath);

            Console.WriteLine("      " + item.OutputDirectory);

            Console.WriteLine("      " + item.OutputBaseName);

 

            if (item.HasErrors() == false)

            {

 

                foreach (PNConversionResultOutputFile outputfile in

                         item.ConversionResult.OutputFiles)

                {

                    Console.WriteLine("    Converted to: " + outputfile.OutputFilePath);

                }

            }

            else

            {

                foreach (PNConversionResultError errorItem in

                         item.ConversionResult.Errors)

                {

                    Console.WriteLine("    Error: " + errorItem.Value);

                }

            }

        }

    }

}

 

 

The console output from the above code is shown below.