如何删除 LRU 文件夹直至有 5GB 可用空间?

发布于 2024-07-27 19:39:02 字数 185 浏览 7 评论 0原文

给定一个文件夹,例如 \\localhost\c$\work\

我想每 15 分钟运行一次 powershell 脚本,以确保有 5GB 的可用空间。

如果< 有 5GB 可用空间,请删除工作中最近最少使用的文件夹,直到有 >5GB 可用空间。

想法?

Given a folder, say \\localhost\c$\work\.

I'd like to run a powershell script every 15 minutes that ensures 5GB of free space is available.

If < 5GB is available, remove the least recently used folder within work until >5GB is available.

Thoughts?

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

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

发布评论

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

评论(2

死开点丶别碍眼 2024-08-03 19:39:02

计划任务,您可以使用任务计划程序(示例

要 你可以使用的脚本

param($WorkDirectory = 'c:\work'
    , $LogFile = 'c:\work\deletelog.txt' )

#Check to see if there is enough free space
if ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
{
    #Get a list of the folders in the work directory
    $FoldersInWorkDirectory = @(Get-ChildItem $WorkDirectory | Where-Object {$_ -is [System.IO.DirectoryInfo]} | Sort-Object -Property LastWriteTime -Descending)
    $FolderCount = 0

    while ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
    {
            #Remove the directory and attendant files and log the deletion
        Remove-Item -Path $FoldersInWorkDirectory[$FolderCount].FullName -Recurse
            "$(Get-Date) Deleted $($FoldersInWorkDirectory[$FolderCount].FullName)" | Out-File -Append $LogFile

            $FolderCount++
    } 
}

To schedule the task, you can use the task scheduler (example here)

For a script you could use

param($WorkDirectory = 'c:\work'
    , $LogFile = 'c:\work\deletelog.txt' )

#Check to see if there is enough free space
if ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
{
    #Get a list of the folders in the work directory
    $FoldersInWorkDirectory = @(Get-ChildItem $WorkDirectory | Where-Object {$_ -is [System.IO.DirectoryInfo]} | Sort-Object -Property LastWriteTime -Descending)
    $FolderCount = 0

    while ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
    {
            #Remove the directory and attendant files and log the deletion
        Remove-Item -Path $FoldersInWorkDirectory[$FolderCount].FullName -Recurse
            "$(Get-Date) Deleted $($FoldersInWorkDirectory[$FolderCount].FullName)" | Out-File -Append $LogFile

            $FolderCount++
    } 
}
我的鱼塘能养鲲 2024-08-03 19:39:02

好吧,这一切都取决于您的文件夹如何“使用”。 如果没有简单的指标,您可以尝试使用 .NET FileSystemWatcher类来查找更改并记住它们以及队列中的时间戳(按访问时间排序)。 然后您可以选择要从该队列中删除的下一个文件夹。 但它肯定不漂亮。

Well, that all depends on how your folders are "used". If there are no easy indicators you can try using the .NET FileSystemWatcher class to look for changes and remember them as well as a timestamp in a queue, ordered by access time. Then you can pick the next folder to delete from that queue. But it's certainly not pretty.

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