本文介绍了Windows bat脚本,如果超过N GB,则获取目录大小,然后删除最旧的文件以恢复空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是蝙蝠脚本的新手,并且满足以下要求:

I'm new to bat scripting and I have following requirement to meet:

如果Dir例如C:\ test大小超过50GB,则脚本应删除最旧的文件,直到目录大小回到50GB或更小.

If Dir eg. C:\test size exceed 50GB, then the script should delete the oldest files until the size of directory goes back to 50GB or little less.

我修改了在中找到的以下脚本使用批处理文件在"MB"中获取目录的大小,但我不确定如何继续执行其余要求.看到到目前为止我所做的微小修改,我将不胜感激:

I have modified the below script I found on Get size of a directory in 'MB' using batch file but I'm not sure how to proceed with the remaining requirements. See the small modifications I have done so far and I would appreciate any input:

@echo off
set limit="50.0"
set Folder="C:\test"
echo The size of %Folder% is
Call :GetSize %Folder%
IF /I "Call :GetSize %Folder%" GEQ "50.0" Echo Overlimit
<Here I would like to delete oldest zip files until the folder size is back to <=50.0GB>


pause
:GetSize
(
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo    Set fso = CreateObject("Scripting.FileSystemObject"^)
echo    Set objFolder= fso.GetFolder(MyFolder^)
echo    GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo    For i = 0 to 4
echo        If iSize ^> 1024 Then
echo            iSize = iSize / 1024
echo        Else
echo            Exit For
echo        End If
echo    Next
echo    FormatSize = Round(iSize,2^)
echo End Function
echo '*******************************************************************
)>%tmp%\Size.vbs
Cscript /NoLogo %tmp%\Size.vbs
Del %tmp%\Size.vbs
Exit /b

推荐答案

这似乎在我的计算机上有效.

This seems to work on my computer.

Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFolder("C:\Users\User\Desktop\New Folder\Stories\Test")

If F.size > 2^30*2 Then

            'Comments on a stupid editor that can't handle tabs
            'Creating an in memory disconnected recordset to sort files by date
        Set rs = CreateObject("ADODB.Recordset")
        With rs
            .Fields.Append "Date", 7
            .Fields.Append "Txt", 201, 5000
            .Open

            For Each Thing in f.files
                .AddNew
                .Fields("Date").value = thing.datelastmodified
                .Fields("Txt").value = thing.path
                .UpDate
            Next
            .Sort = "Date Desc"
            Do While not .EOF
                fso.deletefile  .Fields("Txt").Value
                If f.size < 2^30*2 then Exit Do
                .MoveNext
            Loop
        End With
End If

PS 2 ^ 30 = 1 GiByte或1 Gig.

PS 2^30 = 1 GiByte or 1 Gig.

这篇关于Windows bat脚本,如果超过N GB,则获取目录大小,然后删除最旧的文件以恢复空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:41