代码之家  ›  专栏  ›  技术社区  ›  Ali

如果超过N GB,则使用Windows bat脚本获取目录大小,然后删除最旧的文件以恢复空间

  •  1
  • Ali  · 技术社区  · 7 年前

    我是bat脚本新手,需要满足以下要求:

    如果目录(如C:\test size)超过50GB,则脚本应删除最旧的文件,直到目录大小恢复到50GB或略小。

    我已修改了在上找到的以下脚本 Get size of a directory in 'MB' using batch file 但我不确定如何继续执行其余的要求。请参阅我迄今为止所做的小修改,如有任何意见,我将不胜感激:

    @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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   ACatInLove    7 年前

    这似乎在我的电脑上起作用。

    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吉比特或1吉比特。