Import Files From A Several Nested Folders
This example will show you how to import files and images from several nested directories. This is very useful when you have stored your images in a structured way and wishes to keep this structure in Perfion.
Besides importing the images, this Action example will also mirror the directory on the file system as a category structure in Perfion, allowing you to keep the structure you defined on the file system.
This example is provided as inspiration only. It must be adapted to your Perfion environment to work. Perfion Support does not assist with such adaptations. Please contact your Perfion Partner or your Perfion consultant if you need assistance.
This Action Map consists of two Action, a parent Action in which you specify the root folder and does the importing, and a child Action that will traverse the file system.
Download the Action here:
Parent Action
In the parent Action you will provide the root folder of the image directories. This Action will also format all the found images and import them.
The first step is the EXECUTE.ACTION command. This will start the child action that finds all of the files in all of the directories. Then wit will format the found files in a format that is accepted by the Perfion Importer and finally import it.
Script For the SELECT:
//Convert Folder structure to Category notation ( xx\yy -> xx|yy)
// Split folders in to ArrayList
string[] temp = Value.ToString().Split('\\');
System.Collections.ArrayList PathElems = new System.Collections.ArrayList();
PathElems.AddRange(temp);
// Remove first and last item in folder list - Modify as needed
// Remove first ( "C:")
PathElems.RemoveAt(0);
// Remove last - the filename
PathElems.RemoveAt(PathElems.Count - 1);
// Rejoin folder list with Pipe tag as connecting tag
Value = String.Join("|",(string[])PathElems.ToArray(typeof(string))); |
Child Action
The child Action is the one that actually will do the traversing of the directories and finding all of the files.
It will start in the root folder and then call itself on each of child folders, continuing the loop in subsequent folders .
Script for EXECUTE.SCRIPT
// Read subdirectories from FROM folder into TO datatable
DataTable outSource;
string InFolder = var["$Folder"].ToString();
string OutTable = P.To.ToString();
// Creates a new output datatable
outSource = new DataTable( "Data" );
Sources.Add( OutTable, outSource ); // Setting the output source name. Has to be done both in the source itself and in the end, when you return it to the Action structure
// Adding new columns to the Output datatable
outSource.Columns.Add("Folder");
// Now find all the subdirectories under this directory.
System.IO.DirectoryInfo root = new System.IO.DirectoryInfo(InFolder);
// Now find all the subdirectories under this directory.
System.IO.DirectoryInfo[] subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
DataRow newRow = outSource.NewRow();
newRow["Folder"] = InFolder + @"\" + dirInfo.Name;
outSource.Rows.Add(newRow); // Adding the newRow to output datatable
}
// Returning outSource to Action
Sources.Set(OutTable, outSource); |