删除6个月以上的文件 [英] Delete files older 6 months

查看:109
本文介绍了删除6个月以上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Msbuild删除文件夹中的文件,这些文件已存在六个月以上-早于六个月.

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

我要使用MsBuild的%ModifiedTime(知名商品元数据)

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

我不想使用海关任务,仅使用msbuild默认值和Microsoft.Sdc.Tasks.我使用的是VS 2008,.net .35.

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

有什么建议吗?

<Target Name="SomeTarget"> 

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

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

</Target> 

推荐答案

认为可以实现此目标,而无需在本机MSBuild 4中使用自定义任务,但是我还没有开始使用它但是,因此无法发表评论.

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.

但是,对于本机MSBuild 3.5,我认为这是不可能的-为了操纵需要分解为代码的日期.您会看到,ModifiedDate元数据在内部是一个字符串-要进行明智的操作,您需要将其转换为日期.

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.

我不确定Sdc任务中的内容-我不喜欢使用它们,因为我更喜欢CommunityTasks,但是即使有了这些任务,我也无法想到任何可行的方法.

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.

自定义MSBuild任务并不是那么可怕-我建议每个(可调整大小的)项目都应具有一个在任何其他解决方案之前构建的解决方案,该解决方案会将包含您的自定义msbuild任务的DLL输出到众所周知的位置(例如,"lib"位于您的源根目录).

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>

当然是YMMV

这篇关于删除6个月以上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆