| 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 *.sdltm, which then need to be exported to *.tmx.
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 level of recursion when iterating thru subfolders. /// </summary> public const int RecursionLevel = 1;
/// <summary> /// This function is used to iterate through the main folder and /// (if applicable) the subfolders to look for *.sdltm files to export. /// </summary> /// <param name="sourceDirectory">Directory to iterate thru.</param> /// <param name="processSubFolders">True if recursive teration thru subfolders is required.</param> public void ProcessDirectory(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) { if (fileName.ToLower().EndsWith(".sdltm")) { // Only process file if it is a TMX import file. Console.WriteLine("Exporting " + fileName); TMExporter exportTm = new TMExporter(); exportTm.Export(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.ProcessDirectory(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.BatchExport.exe c:\tm /y
/// <summary> /// Main entrance point of the program. /// </summary> /// <param name="args">Contain string parameters that are passed via the 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.BatchExport.exe source /y"); Console.WriteLine("source path to input folder"); Console.WriteLine("/y should process subfolders"); Console.WriteLine("This application uses a hard-coded recursion level of up to 10 sub-folders."); return; } if (!String.IsNullOrEmpty(args[0]) && !Directory.Exists(args[0])) { Console.WriteLine("Specify valid input directory. Press ENTER to exit."); return; } mainPath = args[0]; if (!String.IsNullOrEmpty(args[1]) && args[1] == "/y") { processSubFolders = true; } try { TMIterator it = new TMIterator(); it.ProcessDirectory(mainPath, processSubFolders); Console.WriteLine(); Console.WriteLine("Batch export 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>Kostiantyn Lukianets</author> // <email>klukianets@sdl.com</email> // <date>2010-09-27</date> // --------------------------------- namespace Sdl.SDK.LanguagePlatform.Samples.BatchExport { using System; using System.IO; /// <summary> /// Represents functionality for importing *.tmx files into FileBasedTranslationMemory. /// </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 level of recursion when iterating thru subfolders. /// </summary> public 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 *.sdltm files to export. /// </summary> /// <param name="sourceDirectory">Directory to iterate thru.</param> /// <param name="processSubFolders">True if recursive teration thru subfolders is required.</param> public void ProcessDirectory(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) { if (fileName.ToLower().EndsWith(".sdltm")) { // Only process file if it is a TMX import file. Console.WriteLine("Exporting " + fileName); TMExporter exportTm = new TMExporter(); exportTm.Export(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.ProcessDirectory(subdir, processSubFolders); } } } #endregion } } } }