提示或PS命令删除x天之前的文件夹

时间:2018-07-13 09:40:53

标签: windows powershell command command-prompt

我正在使用inno安装程序构建安装程序,我想添加计划任务以使用单个命令清除X天之前的日志文件夹。 我正在搜索一些使powershell或提示命令生效的示例,但没有一个起作用。

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

谢谢

1 个答案:

答案 0 :(得分:1)

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

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
}

您只需要将此脚本添加到启动文件夹并更改值(例如,文件路径,文件使用期限,睡眠)。您还可以将数据附加到文本文件中。

我从以下帖子开始: How can I check if a file is older than a certain time with PowerShell?

谢谢

卡尔文

编辑:格式化

相关问题