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 1034425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:23:23+00:00 2026-05-16T14:23:23+00:00

I want delete files in a folder, which are more than six months old

  • 0

I want delete files in a folder, which are more than six months old -older than 6 months-using Msbuild.

I want use %ModifiedTime (Well-known Item Metadata) of MsBuild

I prefer not use customs Tasks, only msbuild default and Microsoft.Sdc.Tasks. I use VS 2008, .net .35.

Any suggestions ?

<Target Name="SomeTarget"> 

<ItemGroup> 
    <FilesToDelete Include="Path\**\*.zip"/> 
</ItemGroup> 

<Delete Files="@(FilesToDelete)" /> 

</Target> 
  • 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-05-16T14:23:24+00:00Added an answer on May 16, 2026 at 2:23 pm

    I think you can achieve this without need to use custom tasks in native MSBuild 4, but I haven’t started playing with that yet, so can’t comment.

    However, as for native MSBuild 3.5 I don’t think it’s possible – in order to manipulate the dates you need to break out into code. You see, the ModifiedDate metadata is internally a string – and to do sensible manipulations you need to convert to a date.

    I’m not sure what is in the Sdc tasks – I don’t use them as I prefer the CommunityTasks, but even with those tasks I can’t think of anything that would work.

    Custom MSBuild tasks aren’t that scary – and I recommend that every (sizeable) project should have a solution that is built before any other solution that outputs a DLL containing your custom msbuild tasks into a well know location (eg a “lib” folder at the root of your source).

    If you can allow this as a solution then here is a task I just knocked up that achieves what you want:

    using System;
    using System.Linq;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    
    namespace Build.MsBuildTasks
    {
        public class FindFilesOlderThan : Task
        {
            [Required]
            public ITaskItem[] Files { get; set; }
    
            public int Months { get; set; }
    
            public int Days { get; set; }
    
            public int Years { get; set; }
    
            [Output]
            public ITaskItem[] MatchingFiles { get; set; }
    
            public override bool Execute()
            {
                var olderThan = DateTime.UtcNow.AddYears(-Years).AddMonths(-Months).AddDays(-Days);
    
                MatchingFiles = (from f in Files
                                 where DateTime.Parse(f.GetMetadata("ModifiedTime")) < olderThan
                                 select f).ToArray();
    
                return true;
            }
        }
    }
    

    You would then use it like so:

    <UsingTask AssemblyFile="$(MSBuildProjectDirectory)\..\lib\Build.MsBuildTasks.dll"
        TaskName="Build.MsBuildTasks.FindFilesOlderThan" />
    
    <Target Name="Purge">
        <ItemGroup>
            <FilesToConsider Include="f:\temp\AzurePackages\**\*.*" />
        </ItemGroup>
    
        <FindFilesOlderThan
            Files="@(FilesToConsider)"
            Months="6">
            <Output
                TaskParameter="MatchingFiles"
                ItemName="FilesToPurge"/>
        </FindFilesOlderThan>
    
    
        <Message Text="FilesToPurge:  @(FilesToPurge)" />
    </Target>
    

    Of course, YMMV

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete content from every file within a folder (Which includes files
I want to delete a folder that contains thousands of files and folders. If
When a user click the .msi, i want a folder called Your Files, which
I want to have a web based admin to upload, delete files and folders
Hey guys i wrote a quick test. I want delete to call deleteMe which
I want to write to/delete a file but sometimes I get a crash if
I just want to see what files were modded/added/deleted between 2 arbitrary revisions. How
I want to delete foo() if foo() isn't called from anywhere.
I want to delete all but the 4 newest directories in my parent directory.
I want to delete all directories and subdirectories under a root directory that are

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.