Edit:
The purpose of this method is to search a specific directory for a given file type, ex. (*.txt, *.pdb, *.exe") then move the file(s) to a given destination directory and delete the original file from the source directory.
Is there a better way to write the following code?:
/// <summary>
/// Moves a specific file type from a given
/// source directory to a destination directory.
/// </summary>
/// <param name="sourceDir">The source directory.</param>
/// <param name="destDir">The destination directory.</param>
/// <param name="fileExt">Type of file to be moved.</param>
private static void MoveFilesToDirectory(string sourceDir, string destDir, string fileExt)
{
try
{
if ( !Directory.Exists(sourceDir) )
{
Console.WriteLine("{0} does not exist.", sourceDir);
return;
}
if ( !Directory.Exists(destDir) )
{
Directory.CreateDirectory(destDir);
}
foreach ( String dir in Directory.GetDirectories(sourceDir) )
{
foreach ( String file in Directory.GetFiles(dir, fileExt) )
{
string fileName = Path.GetFileName(file);
string moveFileTo = Path.Combine(destDir, fileName);
if ( !File.Exists(moveFileTo) )
{
Directory.Move(file, moveFileTo);
}
File.Delete(file);
}
MoveFilesToDirectory(dir, destDir, fileExt);
}
}
catch ( IOException ex )
{
Console.WriteLine(ex.Message);
}
}