Delete files older 6 months

最后都变了- 提交于 2019-12-12 14:55:41

问题


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:


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



来源:https://stackoverflow.com/questions/3511706/delete-files-older-6-months

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!