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

  • Home
  • SEARCH
  • 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 like this: wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep1.fastq.trim.tags.sam wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep2.fastq.trim.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim00.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim01.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim02.tags.sam wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep2.fastq.trim00.tags.sam
I have a list of files that I would like analyze. They are all
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 in an array where the filename is the
I have a list of files that I'm trying to copy and move (using
I have a list of files URLS where I want to download them: http://somedomain.com/foo1.gz
I have a list of files of which I want to add a line
I have a list of files, which need to be read, in chunks, into
I have a list of files that needs to be processed at the same

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.