Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

I wanted to create batch to backup source files from my Projects folder, but also exclude some folders that I do not use.

I was amased how quickly I was able to do it using MSBuild project without any previous experience with the tool.

The purpose of the project is to copy files with specified extensions from specified folder and all subfolders, excluding some subfolders

The Destination folder name is generated based on current time.

The batch file to start utility:

call "C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat" x86
msbuild.exe /v:m BackupSrc.xml

The BackupSrc.xml project file is the following:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup >

    <ExcludeFolders>C:\Projects\Samples\**\*.*;C:\Projects\WebSites\**\*.*ExcludeFolders>

    <SrcPath>C:\Projects\**\*.SrcPath>

  </PropertyGroup>

  <ItemGroup>

     

      <MySourceFiles

        Include="$(SrcPath)aspx;$(SrcPath)vb;$(SrcPath)GIF;$(SrcPath)JPG;$(SrcPath)sln;$(SrcPath)CS;$(SrcPath)??Proj;$(SrcPath)cpp;$(SrcPath)h;$(SrcPath)config"

               Exclude="$(ExcludeFolders)" />

      <MySourceFiles

        Include="$(SrcPath)resx;$(SrcPath)SQL;$(SrcPath)DOC;$(SrcPath)rtf;$(SrcPath)bat;$(SrcPath)cmd;$(SrcPath)cst;$(SrcPath)htm;$(SrcPath)html;$(SrcPath)txt"

               Exclude="$(ExcludeFolders)" />

    </ItemGroup>

    <UsingTask TaskName="FolderName"

                  AssemblyFile="C:\Projects\Utilities\MSBuild\CustomTasks\bin\Debug\CustomTasks.dll" />

    <Target Name="CopyFiles">

      <FolderName>

        <Output TaskParameter="FolderNameAsNow" PropertyName="FolderDate" />

      </FolderName>

      <Copy

          SourceFiles="@(MySourceFiles)"

          DestinationFiles="@(MySourceFiles->'C:\Backups\VS2005Projects\$(FolderDate)\%(RecursiveDir)%(Filename)%(Extension)')"

        />

   </Target>

   

 </Project>

 

Note that XML is slightly corrupted by HTML editor that Iam using for blogging. 

 

 

I've also created a CusomTasks.DLL to create a FolderName by formatting  the current date.

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace CustomTasks

{

    using Microsoft.Build.Framework;

    using Microsoft.Build.Utilities;

 

        public class FolderName : Task

        {

            public override bool Execute()

            {

                myFolderNameAsNow = GetDateTimeFolder();

                return true;

            }

 

            private string myFolderNameAsNow;

            [Output] // attribute is important

            public string FolderNameAsNow

            {

                get { return myFolderNameAsNow; }

                set { myFolderNameAsNow = value; }

            }

            private static string GetDateTimeFolder()

            {

                return "F_" + DateTime.Now.ToString("yyMMdd_HHmm");

            }

 

    }

}

MSBuild Related links: MSBuild Community Tasks Project ,

Sdc.Tasks on gotdotnet,

subtree copy task,

SetMetadata sample on Visual Studio MSBuild forums, 

Microsoft Build Sidekick

 

Less related to the article links about MSBuild:
How to call the same target multiple times at different build stages?

 

Alternatively see free

 

ROBOCOPY.exe (XP Resource Kit/Standard Vista command)

JaBack - Automated Data Backup Software, File Backup Software 
Posted on Friday, February 17, 2006 1:42 PM | Back to top


Comments on this post: Backup source files using MSBuild project.

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net