Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:46:11+00:00 2026-06-05T01:46:11+00:00

I have a list of files generated on each build inside a directory C:\BuildArtifacts

  • 0

I have a list of files generated on each build inside a directory C:\BuildArtifacts

The Contents of a directory looks like this:

TestBuild-1.0.0.1.zip
TestBuild-1.0.0.2.zip
TestBuild-1.0.0.3.zip
TestBuild-1.0.0.4.zip
TestBuild-1.0.0.5.zip
TestBuild-1.0.0.6.zip

Now, with each incremental build, I just want to retain two recent artifacts and delete the rest. So, in this example, I want to retain TestBuild-1.0.0.5.zip and TestBuild-1.0.0.6.zip

How can I do it with MSBuild?

Note:

I have managed to fetch the above list in an item

 <Exec WorkingDirectory="$(Artifacts)\.." Command="dir /B /A:-D /O:-N" Outputs="ArchiveFileList" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:46:13+00:00Added an answer on June 5, 2026 at 1:46 am

    Well, we wrote a custom task to sort the files by their name and then output the list of the files to delete (excluding the first two in the list) into an Item

    Custom Task:

     <UsingTask
        TaskName="Cleanup"
        TaskFactory="CodeTaskFactory"
        AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
        <ParameterGroup>
          <TargetPath ParameterType="System.String" Required="true"/>
          <BackupLength ParameterType="System.Int32" Required="true"/>
          <FilesToExclude ParameterType="System.String[]" Output="true" />
          <FilesToDelete ParameterType="System.String[]" Output="true" />
        </ParameterGroup>
        <Task>
          <Using Namespace="System.IO" />
          <Using Namespace="System.Linq" />
          <Code Type="Fragment" Language="cs">
            <![CDATA[
            var diInfo = new DirectoryInfo(TargetPath);
                if (diInfo.Exists)
                {
                    var fiInfo = diInfo.GetFiles().OrderByDescending(file => file.Name);
    
                    FilesToExclude = fiInfo.Take(BackupLength).Select(file => file.FullName).ToArray();
                    FilesToDelete = fiInfo.Skip(BackupLength).Select(file => file.FullName).ToArray();
                }
            ]]>
          </Code>
        </Task>
      </UsingTask>
    

    Usage:

    <!-- Clean old archives. Keep the recent two and deletes rest. -->
    <Cleanup TargetPath="$(PackageRoot)" BackupLength="2">
      <Output TaskParameter="FilesToDelete" ItemName="FilesToClean" />
    </Cleanup>
    
    <Message Text="Cleaning Old Archives" Importance="High" />
    <Delete Files="@(FilesToClean)" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of files in a directory in the format of YearMonthDay
I have this list of files on a Linux server: abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28 abc.log.2012-03-29
I have list of files which contain particular patterns, but those files have been
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
I have a list of files stored in a text file called tempfile.txt. eg
I have a list of files, the names of these files are are made
I have a list of files [input1.txt,input2.txt,input3.txt] I want to create a new file
I have a list of files (in array): C:\Data\..\Users\admin\history.dat C:\\Users\admin\..\user\history.dat C:\Users\..\Data\debug.log How can I
I have a list of files in an array where the filename is the
I have a variable with a list of prerequisites in varying directories, each specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.