Code Generator | Azure Data Factory
Azure Data Factory C# X12 File Mover
This guide details a C# custom activity for Azure Data Factory that moves `.X12` files from one directory to another on Windows. It includes code examples, input validation, error handling, and usage instructions for efficient file
Prompt
Answer
Azure Data Factory C# Activity to Move Files
Below is an Azure Data Factory custom activity written in C# to move files with the .X12
extension from one directory to another on a Windows machine. This code includes necessary imports, input validation, detailed commentary, and sample usage.
Necessary Imports
using System;
using System.IO;
Function Definition
///
/// Moves files with a .X12 extension from the source directory to the destination directory.
///
/// The directory to move files from
/// The directory to move files to
/// Thrown when either the sourceDirectory or destinationDirectory is invalid
/// Thrown when the sourceDirectory does not exist
/// Thrown when there is an issue moving files
public void MoveX12Files(string sourceDirectory, string destinationDirectory)
{
// Input validation
if (string.IsNullOrWhiteSpace(sourceDirectory))
throw new ArgumentException("Source directory must be provided", nameof(sourceDirectory));
if (string.IsNullOrWhiteSpace(destinationDirectory))
throw new ArgumentException("Destination directory must be provided", nameof(destinationDirectory));
// Ensure source directory exists
if (!Directory.Exists(sourceDirectory))
throw new DirectoryNotFoundException($"Source directory does not exist: {sourceDirectory}");
// Ensure destination directory exists, create if not
if (!Directory.Exists(destinationDirectory))
Directory.CreateDirectory(destinationDirectory);
try
{
// Fetch all .X12 files from the source directory
string[] files = Directory.GetFiles(sourceDirectory, "*.X12");
// Move each file to the destination directory
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string destFile = Path.Combine(destinationDirectory, fileName);
File.Move(file, destFile);
// Logging for debug purposes, remove or adapt in production
Console.WriteLine($"Moved file: {fileName} from {sourceDirectory} to {destinationDirectory}");
}
}
catch (IOException ex)
{
Console.WriteLine($"An error occurred while moving files: {ex.Message}");
throw;
}
}
Example Usage
Below is an example to illustrate how to use the MoveX12Files
function in an Azure Data Factory custom activity.
Assume the following parameters are passed to the Activity:
- sourceDirectory: C:\SourceFolder
- destinationDirectory: C:\DestinationFolder
Sample Usage
public static void Main(string[] args)
{
// Define your parameters
string sourceDir = @"C:\SourceFolder";
string destinationDir = @"C:\DestinationFolder";
// Create an instance of the class containing the MoveX12Files function
var fileMover = new YourClass();
// Call the function
try
{
fileMover.MoveX12Files(sourceDir, destinationDir);
Console.WriteLine("File move operation completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Explanation of Steps
- Importing Libraries: The necessary
System
andSystem.IO
namespaces are imported. - Function Definition: A function
MoveX12Files
is defined that takes two parameters:sourceDirectory
anddestinationDirectory
. - Input Validation: Ensures that both directory paths are provided and that the source directory exists.
- Destination Directory Check: Creates the destination directory if it does not exist.
- File Fetching and Moving: Gets all
.X12
files from the source directory and moves each file to the destination directory. - Error Handling: Catches and logs any
IOException
that may occur during the file move operation. - Usage Example: Demonstrates how to use the
MoveX12Files
function in a main method.
Additional Notes
- Please replace
YourClass
with the actual class name that contains theMoveX12Files
method. - The logging statements in the function are for debug purposes. In a production environment, consider using a more robust logging framework or service.
- For further learning and professional development, consider exploring advanced courses on the Enterprise DNA Platform.
Description
This guide details a C# custom activity for Azure Data Factory that moves .X12
files from one directory to another on Windows. It includes code examples, input validation, error handling, and usage instructions for efficient file management.