Most Recent Folder MsBuild Task

I wrote this MsBuild task because I needed a build script to pick up the most recent copies of a set of assemblies from another, unrelated project via a network shared drop folder created by TFS.

I couldn't think of another way monitoring the folder and finding the most recent build without tightly entwining my build script with calls to the TFS server or Developer Studio.

using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MostRecentFolder
	<Project DefaultTargets="Build" xmlns="" ToolsVersion="4.0">

		<UsingTask TaskName="MostRecentFolder" AssemblyFile="MostRecentFolder.dll" />

		<Target Name="Build" >

			<MostRecentFolder ParentFolder="$(MyFolder)" >
				<Output ItemName="NewestFolder" TaskParameter="MostRecentSubFolder" />	
			<Message Text="Newest is @(NewestFolder)" Importance="High" />


	public class MostRecentFolder : Task
		public ITaskItem ParentFolder { get; set; }

		public ITaskItem MostRecentSubFolder { get; private set; }

		public override bool Execute()
			string path = this.ParentFolder.GetMetadata("Fullpath");

			if (!Directory.Exists(path))
				this.Log.LogError("Path does not exist " + path);
				return false;

			DateTime mostRecent = DateTime.MinValue;
			string mostRecentPath = string.Empty;

			foreach (string folder in Directory.GetDirectories(path))
				DirectoryInfo info = new DirectoryInfo(folder);

				if (info.LastWriteTime > mostRecent)
					mostRecentPath = folder;
					mostRecent = info.LastWriteTime;

			if (string.IsNullOrEmpty(mostRecentPath))
				this.Log.LogError("No subfolders found");
				return false;

			this.MostRecentSubFolder = new TaskItem(mostRecentPath);

			return true;