| Looping through the Folder(s) |
The user of your sample application is required to enter a main path. The application then loops through the files in that path and any files contained in sub-folders of the main path. The application logic for traversing the folders and sub-folders will be implemented in a dedicated iterator class. This class will be used both for looping through all *.tmx files to import into the master TMs.
Add a class called TmIterator to your project. At the beginning of the class, declare two constants, one to specify how deep into the sub-folder structure the recursion should go. For example, we will hard-code the depth to 10, so that the application will go down to 10 sub-folder levels. The second constant sets the recursion level, which should be 1:
/// <summary> /// Determines how deep in the sub-folder structure the application should go. /// </summary> public const int Depth = 10; /// <summary> /// Determines current recursion level. /// </summary> private const int RecursionLevel = 1;
/// <summary> /// This function is used to iterate through the main folder and (if applicable) the subfolders to look for *.tmx import files. /// </summary> /// <param name="sourceDirectory">Directory to search in.</param> /// <param name="processSubFolders">True if subfolder processing required.</param> public void ProcessDir(string sourceDirectory, bool processSubFolders)
Within the function implement an if, which makes the function loop through all sub-folders until the recursion level has reached the maximum depth.
// Loop until the recursion level has reached the // maximum folder depth. if (RecursionLevel <= Depth)
// Retrieve the names of the files found in the given folder. string[] fileEntries = Directory.GetFiles(sourceDirectory); foreach (string fileName in fileEntries) { // Only process file if it is a TMX import file. if (fileName.ToLower().EndsWith(".tmx")) { Console.WriteLine("Importing " + fileName); TMImporter importTmx = new TMImporter(); importTmx.Import(fileName); } }
// Self-recursion to loop through the folder structure until // the folder depth has reached the recursion level value. if (processSubFolders) { string[] subdirEntries = Directory.GetDirectories(sourceDirectory); foreach (string subdir in subdirEntries) { if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { this.ProcessDir(subdir, processSubFolders); } } }
The ProcessDir function needs to be called from Main in the Program class as shown below. The user provides two parameters:
Sdl.SDK.LanguagePlatform.Samples.BatchImport.exe c:\tm /y
/// <summary> /// Main entrance point of the application. /// </summary> /// <param name="args">String arguments passed via command line.</param> public static void Main(string[] args) { string mainPath = string.Empty; bool processSubFolders = false; if (args.Length != 2) { Console.WriteLine("Usage:"); Console.WriteLine("Sdl.SDK.LanguagePlatform.Samples.BatchImport.exe source /ps"); Console.WriteLine("source path to input folder"); Console.WriteLine("/ps should process subfolders"); Console.WriteLine("This application uses a hard-coded recursion level of up to 10 sub-folders."); Console.WriteLine("The master TMs are created in a hard-coded location, i.e.: c:\\MasterTMs"); return; } if (!String.IsNullOrEmpty(args[0]) && !Directory.Exists(args[0])) { Console.WriteLine("Specify a valid input directory. Press ENTER to exit."); return; } mainPath = args[0]; if (!String.IsNullOrEmpty(args[1]) && args[1] == "/ps") { processSubFolders = true; } try { TMIterator it = new TMIterator(); it.ProcessDir(mainPath, processSubFolders); Console.WriteLine(); Console.WriteLine("Batch import finished. Press ENTER to exit."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Press ENTER to exit."); Console.ReadLine(); } }
The complete class should now look as shown below:
// --------------------------------- // <copyright file="TMIterator.cs" company="SDL International"> // Copyright 2010 All Right Reserved // </copyright> // <author>Patrik Mazanek</author> // <email>pmazanek@sdl.com</email> // <date>2010-09-27</date> // --------------------------------- namespace Sdl.SDK.LanguagePlatform.Samples.BatchImporter { using System; using System.IO; /// <summary> /// Represents class able to iterate thru disk directory tree. /// </summary> public class TMIterator { #region "constants" /// <summary> /// Determines how deep in the sub-folder structure the application should go. /// </summary> public const int Depth = 10; /// <summary> /// Determines current recursion level. /// </summary> private const int RecursionLevel = 1; #endregion #region "function" /// <summary> /// This function is used to iterate through the main folder and (if applicable) the subfolders to look for *.tmx import files. /// </summary> /// <param name="sourceDirectory">Directory to search in.</param> /// <param name="processSubFolders">True if subfolder processing required.</param> public void ProcessDir(string sourceDirectory, bool processSubFolders) #endregion { #region "scan" // Loop until the recursion level has reached the // maximum folder depth. if (RecursionLevel <= Depth) #endregion { #region "ProcessFiles" // Retrieve the names of the files found in the given folder. string[] fileEntries = Directory.GetFiles(sourceDirectory); foreach (string fileName in fileEntries) { // Only process file if it is a TMX import file. if (fileName.ToLower().EndsWith(".tmx")) { Console.WriteLine("Importing " + fileName); TMImporter importTmx = new TMImporter(); importTmx.Import(fileName); } } #endregion #region "recursion" // Self-recursion to loop through the folder structure until // the folder depth has reached the recursion level value. if (processSubFolders) { string[] subdirEntries = Directory.GetDirectories(sourceDirectory); foreach (string subdir in subdirEntries) { if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { this.ProcessDir(subdir, processSubFolders); } } } #endregion } } } }