简体   繁体   English

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

[英]prompt or PS command to delete folders older than x days

i'm building a setup with inno setup and i'd like to add scheduled task to clean log folders older than X days with single command. 我正在使用inno安装程序构建安装程序,我想使用单个命令添加计划任务以清理X天之前的日志文件夹。 I'm searching for some example to make powershell or prompt command, but none works. 我正在搜索使powershell或提示命令的示例,但没有任何效果。

Can you help me to find best way? 你能帮我找到最好的方法吗?

Thanks 谢谢

I don't have much time to research this but if you would like to search for a file within a folder location continuously covering a specific time-frame you can use the following script; 我没有太多时间来研究此问题,但是如果您想在连续覆盖特定时间范围的文件夹位置内搜索文件,可以使用以下脚本;

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
}

You would just need to add this script to your start-up folder and change the values (EG file path, file age, sleep). 您只需要将此脚本添加到启动文件夹并更改值(例如,EG文件路径,文件使用期限,睡眠)。 You can also append the data to a text file. 您也可以将数据附加到文本文件。

I started with the following post: How can I check if a file is older than a certain time with PowerShell? 我从以下文章开始: 如何使用PowerShell检查文件是否早于特定时间?

Thanks, 谢谢,

Calvin 卡尔文

Edit: Formatting 编辑:格式化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM