Rename files and Folders with Regex

Jun 19, 2009 at 10:50 PM

I am creating a folder structure from a prototype structure and want to rename where the file/folder name matches a simple replacement text string. For example:

 C:\PrototypeName.Module\SomePrototypeNameForm.cs

becomes

C:\NewName.Module\SomeNewNameForm.cs

with a regex PrototypeName  replaced with NewName. Is there a task or tasks to help me do this? I have searched the documentation, but it seems like there isn't. Have I missed something?

Coordinator
Jun 23, 2009 at 1:07 PM

Hi

I don't believe there is a task to address that right now. Mike

Jun 23, 2009 at 6:49 PM

Thanks Mike. I have used a combination of a Community Task and an ExtensionPack task:

	<Target Name="MoveFiles">
		<ItemGroup>
			<AllFiles Include="$(SolutionPath)\**\*.*" />
		</ItemGroup>
		<MSBuild.Community.Tasks.RegexMatch Expression="$(PrototypeName)" Input="%(AllFiles.FullPath)">
			<Output TaskParameter="Output" ItemName="MatchedFiles" />
		</MSBuild.Community.Tasks.RegexMatch>
		<MSBuild.ExtensionPack.Framework.TextString TaskAction="Replace" NewValue="$(SolutionName)" OldString="%(MatchedFiles.FullPath)" OldValue="$(PrototypeName)">
			<Output TaskParameter="NewString" ItemName="NewFiles" />
		</MSBuild.ExtensionPack.Framework.TextString>
		<Copy SourceFiles="@(MatchedFiles)" DestinationFiles="@(NewFiles)" />
		<Delete Files="@(MatchedFiles)" />
	</Target>

(I am cloning a visual studio solution called PrototypeName)

Let me know if you spot a better way:-)

Coordinator
Jun 23, 2009 at 8:35 PM

If its working for you, great! You may be able to use the FindUnder task instead of the community task. It depends on your regex. 3.5.4.0 will ship with a Get TaskAction for folder which supports full regex.

Mike