删除 6 个月前的文件

发布于 2024-09-15 09:34:59 字数 410 浏览 5 评论 0原文

我想使用 Msbuild 删除文件夹中超过 6 个月前的文件 - 超过 6 个月的文件。

我想使用 MsBuild 的 %ModifiedTime (众所周知的项目元数据)

我不喜欢使用自定义任务,只使用 msbuild 默认值和 Microsoft.Sdc.Tasks。我使用 VS 2008,.net .35。

有什么建议吗?

<Target Name="SomeTarget"> 

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

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

</Target> 

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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2024-09-22 09:35:00

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

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

我不确定 Sdc 任务中有什么 - 我不使用它们,因为我更喜欢 CommunityTasks,但即使使用这些任务,我也想不出任何可行的方法。

自定义 MSBuild 任务并不那么可怕 - 我建议每个(相当大的)项目都应该有一个在任何其他解决方案之前构建的解决方案,该解决方案将包含自定义 msbuild 任务的 DLL 输出到众所周知的位置(例如“lib”文件夹)在源的根部)。

如果您可以允许将此作为​​解决方案,那么这是我刚刚完成的一项任务,可以实现您想要的目标:

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;
        }
    }
}

然后您可以像这样使用它:

<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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文