首页 > 解决方案 > 提示或 PS 命令删除超过 x 天的文件夹

问题描述

我正在使用 inno setup 构建设置,我想添加计划任务以使用单个命令清理早于 X 天的日志文件夹。我正在寻找一些示例来制作 powershell 或提示命令,但没有一个有效。

你能帮我找到最好的方法吗?

谢谢

标签: windowspowershellcommandcommand-prompt

解决方案


我没有太多时间来研究这个,但是如果您想在文件夹位置中搜索一个连续覆盖特定时间范围的文件,您可以使用以下脚本;

while($true){

    # You may want to adjust these

    $fullPath = "C:\temp\_Patches\Java\Files\x86\Source"
    $numdays = 5
    $numhours = 10
    $nummins = 5

    function ShowOldFiles($path, $days, $hours, $mins)
    {
        $files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
    if ($files -ne $NULL)
    {
        for ($idx = 0; $idx -lt $files.Length; $idx++)
        {
            $file = $files[$idx]
            write-host ("Old: " + $file.Name) -Foregroundcolor Red
            Start-Sleep -s 10
        }
    }
}

ShowOldFiles $fullPath $numdays $numhours $nummins
}

您只需将此脚本添加到您的启动文件夹并更改值(EG 文件路径、文件年龄、睡眠)。您还可以将数据附加到文本文件中。

我从以下帖子开始: 如何使用 PowerShell 检查文件是否早于某个时间?

谢谢,

卡尔文

编辑:格式化


推荐阅读